everyone.
I have a string like this
String message = "This is the new message or something like that, OK";
And I want to split it into array
String[] dic = {"this", "is", "the", "new", "message", "or", "something", "like", "that", "OK"};
I used
message = message.split("\\s+");
The problem was that it contained "that," not "that" like I want. Please teach my how to solve it. Thanks
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
To split a string on newlines, you can use the regular expression '\r?\ n|\r' which splits on all three '\r\n' , '\r' , and '\n' . A better solution is to use the linebreak matcher \R which matches with any Unicode linebreak sequence. You can also split a string on the system-dependent line separator string.
To split a string by multiple spaces, call the split() method, passing it a regular expression, e.g. str. trim(). split(/\s+/) . The regular expression will split the string on one or more spaces and return an array containing the substrings.
You can do
String[] dic = message.split("\\W+");
The \\W
means not an alphanumeric character.
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