user1052004
user1052004

Reputation: 93

How does String.split work?

Why does the following code return the output below? I would expect that 2 and 3 provide the same string splitting of 1.

    Log.d(TAG, " 1 ---------------------------");
String originalText = "hello. .hello1";

Pattern p = Pattern.compile("[a-zA-Z]+|\\s|\\W|\\d");
Matcher m = p.matcher(originalText);

 while (m.find()) { 
     Log.d(TAG, m.group(0)); 
 }
 Log.d(TAG, "2 --------------------------- " + originalText);

String [] scrollString = p.split(originalText);

int i;      
for (i=0; i<scrollString.length; i++)
    Log.d(TAG, scrollString[i]);

Log.d(TAG, "3 --------------------------- " + originalText);

scrollString = originalText.split("[a-zA-Z]+|\\s|\\W|\\d");


for (i=0; i<scrollString.length; i++)
    Log.d(TAG, scrollString[i]);

OUTPUT:

1 ---------------------------

hello

.

.

hello

1

2 ---------------------------

3 --------------------------- 

Upvotes: 1

Views: 2972

Answers (4)

user177800
user177800

Reputation:

Your subject doesn't match what you are asking.

The Subject asks about String.split() you are doing Pattern.split() which one do you really want help with?

When using String.split(); you pass in the regular expression to apply to the string, not the string you want to split!

JavaDoc for String.split();

final String s = "this is the string I want to split";
final String[] sa = s.split(" ");

you are calling .split on p ( Pattern.split(); )

Pattern p = Pattern.compile("[a-zA-Z]+|\\s|\\W|\\d");
String [] scrollString = p.split(originalText);

these too methods have different behaviors.

Upvotes: 1

Ingo
Ingo

Reputation: 36329

No. Every character in your string is covered by the split pattern, hence taken as something you don't want. Therefore, you get the empty result.

You can imagine that your pattern first finds "hello", then split hopes to find something, but alas!, it finds another "separation" character.

Upvotes: 0

Thomas
Thomas

Reputation: 88707

No. 1 will find the pattern and return that, whereas No. 2 and 3 will return the text in between the found pattern (which serves as the delimiter in those cases).

Upvotes: 1

ratchet freak
ratchet freak

Reputation: 48196

the split() methods don't add the captured part of the string (the delimiter) to the result array

if you want the delimiters you'll have to play with lookahead and lookbehind (or use version 1)

Upvotes: 0

Related Questions