How to split a delimited string into its elements

A

Angus

Hello

I have a semi-colon delimited string eg apple;pear;banana;pineapple; and
want to extract each 'element' of the string. All I really want to do is
print each individual element. But when I get the code working I want to
use in a Java applet so only want to use up to Java 1.1

How would I achieve this?

Angus
 
D

Daniel Dyer

Hello

I have a semi-colon delimited string eg apple;pear;banana;pineapple; and
want to extract each 'element' of the string. All I really want to do is
print each individual element. But when I get the code working I want to
use in a Java applet so only want to use up to Java 1.1

How would I achieve this?

Angus

If you don't have to worry about escape characters or special treatment of
semi-colons within quotes, just use StringTokenizer. Search on Google
Groups for last week's thread about parsing CSV files for more complete
solutions.

Dan.
 
I

IchBin

Angus said:
Hello

I have a semi-colon delimited string eg apple;pear;banana;pineapple; and
want to extract each 'element' of the string. All I really want to do is
print each individual element. But when I get the code working I want to
use in a Java applet so only want to use up to Java 1.1

How would I achieve this?

Angus

Try using String split(). The last I heard Sun was trying to move away
from StringTokenizer to this string function. Example:

String target = "apple;pear;banana;pineapple;";
String[] data = target.split(";");
for (String item : data)
System.out.println(item );

--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
D

Daniel Pitts

IchBin said:
Angus said:
Hello

I have a semi-colon delimited string eg apple;pear;banana;pineapple; and
want to extract each 'element' of the string. All I really want to do is
print each individual element. But when I get the code working I want to
use in a Java applet so only want to use up to Java 1.1

How would I achieve this?

Angus

Try using String split(). The last I heard Sun was trying to move away
from StringTokenizer to this string function. Example:

String target = "apple;pear;banana;pineapple;";
String[] data = target.split(";");
for (String item : data)
System.out.println(item );

Except, the OP specifically said Java 1.1, and split is relatively
knew.
Having said that, there is usually no good reason to stay with Java 1.1
these days, he should indeed use split for the simple case.
 
T

trippy

Angus took the said:
Hello

I have a semi-colon delimited string eg apple;pear;banana;pineapple; and
want to extract each 'element' of the string. All I really want to do is
print each individual element. But when I get the code working I want to
use in a Java applet so only want to use up to Java 1.1

How would I achieve this?

origString.split(";");


--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "All I Really Want" -- Alanis Morissette

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"
 
D

Dag Sunde

Daniel said:
IchBin said:
Angus said:
Hello

I have a semi-colon delimited string eg
apple;pear;banana;pineapple; and want to extract each 'element' of
the string. All I really want to do is print each individual
element. But when I get the code working I want to use in a Java
applet so only want to use up to Java 1.1

How would I achieve this?

Angus

Try using String split(). The last I heard Sun was trying to move
away from StringTokenizer to this string function. Example:

String target = "apple;pear;banana;pineapple;";
String[] data = target.split(";");
for (String item : data)
System.out.println(item );

Except, the OP specifically said Java 1.1, and split is relatively
knew.
Having said that, there is usually no good reason to stay with Java
1.1 these days, he should indeed use split for the simple case.

Here's a function I used in a 1.1 applet once...

/**Splits a string into a list of strings
* @param - source, String original string to be split
* @param - splitAt, token to split at
* @return - Returns a String array containing the sub elements
*/
protected String[] split(String source, String splitAt)
{
int cElements = 0;
for (int i = 0; i < source.length(); i++)
{
if(source.charAt(i) == splitAt.charAt(0))
cElements+=1;
}
String tmpResult[] = new String[cElements+1];

int prevIndex = 0;
for(int i = 0; i <= cElements; i++)
{
int curIndex = source.indexOf(splitAt,prevIndex+1);
if(curIndex == -1)
curIndex = source.length();

tmpResult = new String(source.substring(prevIndex !=0 ? prevIndex+1
: prevIndex , curIndex));
prevIndex = curIndex;
}

return tmpResult;
}
....

String target = "apple;pear;banana;pineapple;";
String[] data = target.split(target,";");
for (int i=0; i<data.length; i++)
System.out.println(data);
 
D

Dag Sunde

Dag Sunde wrote:
...

String target = "apple;pear;banana;pineapple;";
String[] data = target.split(target,";");
for (int i=0; i<data.length; i++)
System.out.println(data);


Oops...

That should read:

String[] data = split(target,";");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top