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