简体   繁体   中英

string.split() and dealing with duplicates

I have a string String test = "-b--b--";

I want an array that has {"", "b", "", "b", ""}

How can I call the string.split() function to obtain this?

EDIT:

I've tried test.split("-") . My tests gave me {"", "b", "", "b"} . Why does the method not include the last emptyspace on my array if it did include the first one between the first pair of hyphens?

Basically I want to get all the characters between the hypens, where String test = --- should give me {"", "", ""}

Its mentioned in the docs

Trailing empty strings not included in the resulting array.


For the desired result change it to

test.split("-",Integer.MAX_VALUE);

This would consider empty strings and so the output would be

"", "b", "", "b", "" , ""

you can try .getCharAt() method of string to your problem.
The following code can help:

char test1[]=new char[test.length()];
for(int i=0;i<test.length();i++)
if(test.getCharAt(i)=='-')
test1[i]=' ';
else
test[i]=test.getCharAt(i);  

hope this will help you....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM