string to words java

www.lautt‮moc.iru‬
string to words java

To split a string into an array of words in Java, you can use the split method of the String class. This method takes a regular expression as an argument and returns an array of strings that are delimited by the regular expression.

Here's an example of how you can use the split method to split a string into an array of words:

String s = "Hello, world!";
String[] words = s.split("\\s+");  // words is ["Hello,", "world!"]

In this example, we're using the regular expression "\\s+" as the delimiter, which matches one or more consecutive whitespace characters. You can use a different regular expression as the delimiter to split the string in a different way.

For example, to split the string at each punctuation character, you can use the following code:

String s = "Hello, world!";
String[] words = s.split("\\p{Punct}");  // words is ["Hello", "world"]

You can find more information about regular expressions and the split method in the Java documentation.

Created Time:2017-10-17 20:18:55  Author:lautturi