Answer the question
In order to leave comments, you need to log in
How to split a java string?
Good afternoon, I use the split() method to split a string into "words", but there is a problem: words in sentences are separated by spaces and there can be more than one space. For example: " Мама мыла раму
". How to be in this situation?
Answer the question
In order to leave comments, you need to log in
The split method accepts a regular expression, respectively, you can write that you need to split by one or more spaces someString.split("\\s+")
, or split by one and filter empty lines from the returned array.
Good afternoon.
The split method accepts a regexp.
String str = "Мама мыла раму";
String[] words = str.split(" ");
String str = "Мама мыла раму";
String[] words = str.split("\\s+");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question