chenwei
asked on
How to use String.split() to split this string?
My string looks as follow:
str = "fist second third forth fifth";
Can I use split() to split it into 5 sub strings, i.e. without containning the spaces?
str = "fist second third forth fifth";
Can I use split() to split it into 5 sub strings, i.e. without containning the spaces?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Explanation: the delimiter is one or more times a space
Or simply just use java.util.StringTokenizer like this:
StringTokenizer st = new StringTokenizer("fist second third forth fifth");
while (st.hasMoreTokens()) {
System.out.println(st.next Token());
}
StringTokenizer st = new StringTokenizer("fist second third forth fifth");
while (st.hasMoreTokens()) {
System.out.println(st.next
}
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Thanks
split("([ ]){+}");
I'll check it for you