P
P
pshevnin2022-04-02 13:53:14
Java
pshevnin, 2022-04-02 13:53:14

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

2 answer(s)
S
Sergey Gornostaev, 2022-04-02
@pshevnin

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.

O
Orkhan, 2022-04-02
Hasanly @azerphoenix

Good afternoon.
The split method accepts a regexp.

String str = "Мама мыла раму";
String[] words = str.split(" ");

https://javarush.ru/groups/posts/2907-method-split-... Are you talking
about this?
If you want to split a string that can have a different number of spaces between words
String str = "Мама      мыла   раму";
String[] words = str.split("\\s+");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question