I'm trying to split a string up into words and punctuation, adding the punctuation to the list produced by the split.
For instance:
String c = "help, me!"
I want out the list to look like is:
['help', ',', 'me', '!']
So, I want the string split at whitespace with the punctuation split from the words. Do you have ideas how to do it?
Try this
String str = "help, me!";
StringTokenizer st = new StringTokenizer(str, ", !", true);
while (st.hasMoreElements()) {
System.out.println(st.nextElement());;
}
Output:
help
,
<- space
me
!
You can do it using regex
as follows:
String d = Str.split("\\W+");
Updated answer for your question:
String d = Str.split("\\b");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With