Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split method in Java

Status
Not open for further replies.

kasuals

Programmer
Apr 28, 2002
100
0
0
US
Is there a method in Java comparable to the SPLIT function in Perl?

What I need to do is split this string for example:

test1~test2~test3~test4

Into an array...

Any ideas? - "Delightfully confusing..." raves The New York Times

-Kas
 
Take a look at the String Tokenizer class. It allows you to split a string up based on tokens that you set in the constructor. For instance:

StringTokenizer st = new StringTokenizer( "test1~test2~test3~test4", "~" );
String array[] = new String[st.countTokens()];
for( int i = 0; i < array.length; i++ )
array = st.nextToken();

Hope that helped.

-gc
 
Actually, it did, but I had already found it in my Java 2 book ;) Thanks though! I really appreciate it! - &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
Careful at what version of Java you are using. There are some known problems with the StringTokenizer, so at work we actually use a perl library to do some of that work. The new release likely took care of that, but I'd check it out.

The problem we were running into was the behaviour when two+ tokens are found in a row. I think it did not return an empty string for that element, and we had a tokenizer in the next one or something. If this could pose a problem, try a test on

test1~~test2~~~test3~test4

and see what it spits out. Have fun!
 
Well, actually I'm using J2ME wireless toolkit... I'm parsing server data directly on the phone. If that doesn't work, I'll just hack together something. - &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
Why don't you just use the String.split( String ) method?

String strTest = &quot;test1~test2~test3&quot;;
String[] ar_strTest = strTest.split( &quot;\\~&quot; );

That seems much easier. ________________________________________
Michael C Flanakin
Indigo Web Systems
michael.flanakin@indigows.com
 
Grrr....I tried the above,
String filename = &quot;test1,test2,test3&quot;;
String patternStr=&quot;\\,&quot;;
String[] fields = filename.split(patternStr);

It comes up with a java error of:
&quot;The method split(java.lang.String) is undefined for the type java.lang.String&quot;

What am I doing wrong?!?!?!?!?!
 
The method &quot;public String[] split(String regex)&quot; has been introduced with Java 1.4.
Type &quot;java -version&quot; in a console window, the output should be something like

java version &quot;1.4.0&quot;
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top