NeedHelp
Hi,
I am facing problem to doing the following this.
I have string s=ab/bc/db/gh
.I have done tokenize but I can get only
String parent=ab
and child =bc
but I want to get my result following way:
String child=gh
String parent=db
Any one can help me ..
Thanks .
morshed
I'm not going to read your question. I'm only responding to tell you that you'll get better help if you communicate more clearly.
1) We know you need help. That's why you're posting here. The subject is supposed to say something about your problem, so that the right people can look at it. I've stopped even reading questions that don't have a meaningful subject.
2) When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
jverda at 2007-7-12 18:55:57 >

> Hi,
> I am facing problem to doing the following this.
> I have string s=ab/bc/db/gh
>
> .I have done tokenize but I can get only
>
> String parent=ab
> and child =bc
> but I want to get my result following way:
> String child=gh
> String parent=db
> Any one can help me ..
>
> Thanks .
>
> morshed
Look into the nextToken() method. You just need to iterate forward to get the String you want.
String method split is often easier to use than StringTokenizer. Give it a try:
public class SplitExample {
public static void main(String[] args) {
String s="ab/bc/db/gh";
String[] parts = s.split("/");
for(String part : parts) {
System.out.println(part);
}
}
}
> String method split is often easier to use than
> StringTokenizer. Give it a try:
> [code]
> public class SplitExample {
>public static void main(String[] args) {
>String s="ab/bc/db/gh";
>String[] parts = s.split("/");
>for(String part : parts) {
> System.out.println(part);
> }
>}
> /code]
ahhh. String.split(). I keep forgetting we do not need StringTokenizer for trivial splits any more.
> ahhh. String.split(). I keep forgetting we do not
> need StringTokenizer for trivial splits any more.
The good news is that you don't have to tie a string around your finger as a reminder.
The API for StringTokenizer: http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html
Has the reminder:
<quote>
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
</quote>