aspose file tools
The moose likes Java in General and the fly likes parsing a string an basis of a delimetre Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "parsing a string an basis of a delimetre" Watch "parsing a string an basis of a delimetre" New topic
Author

parsing a string an basis of a delimetre

Nrapendra Sharma
Ranch Hand

Joined: Jun 16, 2008
Posts: 31
hi all,
I m trying to parse a string
";;67, Punjabi des Cinquante Otages;Nantes;;44000;France"
with ";" as delimeter
I have used StringTokenizer for this purpose

for (StringTokenizer t = new StringTokenizer(aValue, ";");
t.hasMoreTokens() {
String token = t.nextToken();
System.out.println(token);
}

i m getting four token instaed of 6
StringTokenizer is ignoring the initial two tokens...
can anyone tell me where i m doing wrong ??


Regards,
Nrapendra Sharma
sameer meher
Greenhorn

Joined: Jun 06, 2008
Posts: 10
Hi,
You will always get 4 tokens here.
StringTokenizer is used to parse a formatted text into set of discrete tokens based on the delimiters you have provided.
If your code is:

you will always get 4 tokens all separated by delimiter.

But if you use :
in your code. Your no of tokens will be 4+6=10.. Becuase here when you specify "true" in the StringTokenized constructor, the no. of tokens will add the delimiter also as a part of discrete sets.
Hope this clarifies your doubt


Regards,<br />Sameer
Nrapendra Sharma
Ranch Hand

Joined: Jun 16, 2008
Posts: 31
Got it

Thanks
Nrapendra
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

You can try String#split(String regex) ..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Nicky,

Welcome to JavaRanch.

Using split() method of String class is better option over StringTokenizer.
API documentation says:
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.
sameer meher
Greenhorn

Joined: Jun 06, 2008
Posts: 10
Hi Rohan,

Thanks man I was not aware of formatted string splitting into segments using regex like this.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

Hi Rohan,


Hi Meh ,

How you feel it ??
My name is Sagar, not Rohan , but part of my surname !!

Hope you get it !!
sameer meher
Greenhorn

Joined: Jun 06, 2008
Posts: 10
Hi Sagar,

You seems to be a very strange character.
Anyways, that was mistakenly written man.
My intension was just to thank you for some information which you have provided.
thats it.
Leave it sir.
Sorry for that.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2896
    
    1

You seems to be a very strange character.


No strange behavior ,dude .. Its matter of respecting others name (see)!!
I just told you , whats my name and what you have written is part of my last name .. I just care to correct you !!

Lets stop here ..



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parsing a string an basis of a delimetre
 
Similar Threads
breaking up a file that has a delimiter
StringTokenizer
how to avoid null pointer exception
StringTokenizer and the "" empty string
next line in swings