This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes doubt in string.split method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "doubt in string.split method " Watch "doubt in string.split method " New topic
Author

doubt in string.split method

vicky chauhan
Ranch Hand

Joined: Nov 27, 2008
Posts: 41
Hello guys,

I have following data:

1||1||Abdul-Jabbar||Karim||1996||1974

I want to delimit the tokens.

Here the delimiter being "||".

My delimiter setter is:



However, String[] tokens = line.split(delimiter); is not giving required result.

Please help!!

Nik
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 156


Why would you start with "\"" and end with "\"" ?
and what if your delimiter doesn't need escape character?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

can you explain what you are trying to do in setDelimiter method?
Roshan Ramesh
Greenhorn

Joined: Jul 12, 2010
Posts: 8
Bit tricky. But still, it should not take much time to resolve.

Assuming that you came here for advise on the possible delimeters, let me know if the below code did helped you.

vicky chauhan
Ranch Hand

Joined: Nov 27, 2008
Posts: 41
Roshan Ramesh wrote:Bit tricky. But still, it should not take much time to resolve.

Assuming that you came here for advise on the possible delimeters, let me know if the below code did helped you.




Here is how I did it:

String delimiter = "||";
Pattern pattern = Pattern.compile(Pattern.quote(delimiter));
String[] tokens = pattern.split(line);

This worked perfectly... and is more efficient too!! Thanks to Ray!!
vicky chauhan
Ranch Hand

Joined: Nov 27, 2008
Posts: 41
Raymond Tong wrote:
Why would you start with "\"" and end with "\"" ?
and what if your delimiter doesn't need escape character?


Thanks Raymond.. I found a solution.. I have posted it in the last comment in this post..
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: doubt in string.split method
 
Similar Threads
StringTokenizer - Doesn't return empty Strings or nulls
Formatting outputs based on values in resultset
Tokenizing with regex pattern. Little confused!
About tokenizer.
StringTokenizer takes only single char: pl help!