Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

breaking a String into a String[]

  Asked By: Durril    Date: Nov 26    Category: Java    Views: 1209
  

I have one huge String that contains the entire contents of a text
file - including all the end-of-line characters.

What I want to do is to break this String up into an array of
Strings with one element in the array for each line in the original
string....

For example [using \n to represent new-lines, if my String origString
is:

"abc\nxyx\ngty\nbvf"

I want this to be converted to:

String x[];

x[0] == "abc\n";
x[1] == "xyx\n";
x[2] == "gty\n";
x[3] == "bvf";

I have been hunting through the String class trying to figure out how
to do this but to no avail... I have tried

doing something along the lines of:

String x[] = origString.split("$");

But this doesn't work...

I've also tried

String x[] = origString.split("^.*$");

but again, no luck...

Can anyone suggest how I go about doing this?

Share: 

 

8 Answers Found

 
Answer #1    Answered By: Marina Smith     Answered On: Nov 26

Use StringTokenizer.....................

 
Answer #2    Answered By: Verner Fischer     Answered On: Nov 26

use StringTokenizer(String s, String delim)

in this case, it would be

StringTokenizer tok = new StringTokenizer(origString, "\n");



 
Answer #3    Answered By: Luz Hayes     Answered On: Nov 26

You can also use the apache string  utilities. StringUtil.split()

 
Answer #4    Answered By: Vidos Fischer     Answered On: Nov 26

here is one example  how to use the split() function:

String s1="Hi, my name is ABCDE. \n I am 25 years old.\n";
String[] result=s1.split("\n");
for(int i=0;i<result.length;i++)
System.out.println(result[i]);

The output is: Line 1:Hi, my name is ABCDE.
Line 2:I am 25 years old.

 
Answer #5    Answered By: Hoor Khan     Answered On: Nov 26

someString.split("\n");
The only thing is that it eats the \n in your String.

One thing that you have to be careful of how you input to that String.
It could be more than possible for \r or \n\r to creep into your input
statements (well in my experience they can). So you might need to
process the line  turning all \n and \r to \n\r and then split  on \n\r
(or turn \r and \n\r to \n) ... although your case might be specific
enough that you don't need to.

As for the screams to use StringTokenizer, I try not to use
StringTokenizer if I can at all afford not to. I find it slow and
cumbersome to use in anything but the most simplistic of cases, and even
then the simple cases can be almost always solved with split();

 
Answer #6    Answered By: Hugo Williams     Answered On: Nov 26

i think u should java.util.StringTokenizer Class...
it has good properties for you.
i had written a code about this.. but right now i dont know where i threw
it...:)
but firstly i stored the text of file in a StringBuffer class... then i split  it
with stringtokenizer class..

 
Answer #7    Answered By: Amelia Schmidt     Answered On: Nov 26

You can use the StringTokenizer. You can set "\" as
your delimeter (or mark for separating the strings)
and transfer each part to an array.

 
Answer #8    Answered By: Kristen Chavez     Answered On: Nov 26

You can use a function like this for the same

String[] ret = new String[16];
//default capacity. Using a java util class  and then converting to
String[]
//should be a better idea instead of allocation of memory at the start.
int len = token.length();
int i = 0 ;
for(int j = 0, k = 0; k != -1; j = k + len)
{
k = str.indexOf(token, j);
String tempStr = str.substring(j, k == -1 ? str.length() :
k).trim();
if(tempStr.length() > 0 ){
ret[i] = tempStr;
i++;
}
}

 
Didn't find what you were looking for? Find more on breaking a String into a String[] Or get search suggestion and latest updates.




Tagged: