String split method

A

Anony!

Hi

Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

for (int i=0;i<result.length;i++)
System.out.println(result);


expect:
abc
def
etc..

but got nothing

AAAAAA
 
O

Owen Jacobson

Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

What you've just said is that [a-z][a-z][a-z] is the pattern that sits
between each match.

Was there something specifically wrong with using substring(start, end) ?
 
A

Anony!

Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

What you've just said is that [a-z][a-z][a-z] is the pattern that sits
between each match.

What should the delimiter be if I want it to break it into substrings of
length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
properly when using Pattern and Match classes.
Was there something specifically wrong with using substring(start, end) ?

no, just not as elegant as the string split function.
 
L

Liz

Anony! said:
Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

Did you try escaping the curley brackets? "[a-z]\{3\}", or perhaps double
slashes.

What you've just said is that [a-z][a-z][a-z] is the pattern that sits
between each match.

What should the delimiter be if I want it to break it into substrings of
length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
properly when using Pattern and Match classes.
Was there something specifically wrong with using substring(start, end)
?

no, just not as elegant as the string split function.
 
O

Owen Jacobson

Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

What you've just said is that [a-z][a-z][a-z] is the pattern that sits
between each match.

What should the delimiter be if I want it to break it into substrings of
length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
properly when using Pattern and Match classes.

It shouldn't. Split *does not* do what you want. In all cases,
split(regex) consumes the text matching the regex. No matter what
delimiter pattern you choose some characters will be lost.
no, just not as elegant as the string split function.

/** Totally untested. */
private static String[] fixedSegments (String s, int length) {
assert (length > 0);

int segments = (s.length() + length - 1) / length;
if (s.length () % length != 0)
++segments; // hold the last partial segment

String[] r = new String[segments];
for (int i = 0; i < segments; ++i)
r = s.substring (i * length,
Math.min ((i + 1) * length, s.length ()));

return r;
}
 
A

andrewh1

Anony! said:
Hi

Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

No it doesn't. You need to read the docs.

The docs for 1.4

***********************************************
split

public String[] split(String regex)

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method
with the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.

The string "boo:and:foo", for example, yields the following results
with these expressions:

Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around
matches of the given regular expression
********************************************************************
Effectively you are getting it to give you the strings separated by (and
not including) any lower case letter in the alphabet. In your case lots
of "".
However since it 'doesn't include trailing empty strings' it should
return an empty array.

I may be wrong, but I doubt using regular expressions is the way to go
if you simply want a string chopped into equal sized fragments
disregarding character content. String.Substring() method is probably
the best.

Seems to me that regular expressions are more for dealing with strings
where string content (ie the character sequence) defines the operations.

Cheers

Andrew
for (int i=0;i<result.length;i++)
System.out.println(result);


expect:
abc
def
etc..

but got nothing

AAAAAA
 
C

Chris Uppal

Anony! said:
no, just not as elegant as the string split function.

Elegance is in the eye of the beholder. To /my/ eye, using regexps when no
regexp is needed looks like unecessary bloat, obfuscation, and the misuse of
heavyweight (and slow[*]) features to perform lightweight operations.

(Not to say that it is /necessarily/ wrong, though, e.g. if I had lots of
string splitting to do, nearly all of which needed full regexp power, then I
would quite probably use it for the simple cases too. Just to keep things
simple and regular.)

[*] I admit I haven't measured the difference.

-- chris
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top