How to Split a String in Java
Sometimes we need to split a string in programming. We suggest String.split (), StringTokenizer, and Pattern.compile () methods.
Spliting a String in Java
public class StringSplitTest < public static void main(String[] arg) < String str = "Welcome:dear guest"; String[] arrOfStr = str.split(":"); for (String a: arrOfStr) System.out.println(a); > >
The output for this will be like this:
Result
Note: Change regex to come to a different result:
E.g. for («e») regex the result will we like this:
There are many ways to split a string in Java. The most common way is using the split() method which is used to split a string into an array of sub-strings and returns the new array.
1. Using String.split ()
The string split() method breaks a given string around matches of the given regular expression. There are two variants of split() method in Java:
This method takes a regular expression as a parameter and breaks the given string around matches of this regular expression regex. By default limit is 0.
Parameter for this is: regex (a delimiting regular expression).
It returns an array of strings calculated by splitting the given string.
Example
Parameters for this are: regex (the delimiting regular expression) and limit (controls the number of times the pattern is applied and therefore affects the length of the resulting array).
This returns the array of strings counted by splitting this string around matches of the given regular expression.
Example
public class StringSplitTest < public static void main(String[] arg) < String str = "Hello:world:hello"; String split[] = str.split("e", 5); for (String s: split) System.out.println(s); > >
The output for the given example will be like this:
Result
Note: Change regex and limit to have different outputs: e.g. («:», 2)-output will be ; («:», -2)-, etc.
Example
public class StringSplitTest < public static void main(String[] arg) < String str = "What are you doing today?"; String split[] = str.split(" ", 0); for (String s: split) System.out.println(s); > >
The output will be the following:
Result
Note: Change regex and limit to have another output: e.g. (» «, 2) — the result will be like this:
Result
public class StringSplitTest < public static void main(String args[]) < String s = " ;String; String; String; String, String; String;;String;String; String; String; ;String;String;String;String"; //String[] strs = s.split("[,\\s\\;]"); String[] strs = s.split("[,\\;]"); System.out.println("Substrings length:" + strs.length); for (int i = 0; i < strs.length; i++) < System.out.println("Str[" + i + "]:" + strs[i]); > > >
Output for this will be like this:
Result
Substrings length:17 Str[0]: Str[1]:String Str[2]: String Str[3]: String Str[4]: String Str[5]: String Str[6]: String Str[7]: Str[8]:String Str[9]:String Str[10]: String Str[11]: String Str[12]: Str[13]:String Str[14]:String Str[15]:String Str[16]:String
2. Using StringTokenizer
In Java, the string tokenizer allows breaking a string into tokens. You can also get the number of tokens inside string object.
Example
import java.util.StringTokenizer; public class StringTokenizerExample < public static void main(String[] args) < StringTokenizer st = new StringTokenizer("A StringTokenizer sample"); // get how many tokens are inside st object System.out.println("Tokens count: " + st.countTokens()); // iterate st object to get more tokens from it while (st.hasMoreElements()) < String token = st.nextElement().toString(); System.out.println("Token = " + token); > > >
Result
Tokens count: 3 Token = A Token = StringTokenizer Token = sample
Note: You can specify the delimiter that is used to split the string. In the above example we have set the delimiter as space (”).
It is also possible to use StringTokenizer with multiple delimiters.
Example
import java.util.StringTokenizer; public class StringTokenizerExample < public static void main(String[] args) < String url = "http://www.w3docs.com/learn-javascript.html"; StringTokenizer multiTokenizer = new StringTokenizer(url, "://.-"); while (multiTokenizer.hasMoreTokens()) < System.out.println(multiTokenizer.nextToken()); >> >
Result
http www w3docs com learn javascript html
Note: In the above-mentioned example :, //, ., — delimiters are used.
3. Using Pattern.compile ()
This method splits the given input sequence around matches of the pattern. Parameter for this is: input — the character sequence to be split.
It returns the array of strings computed by splitting the input around matches of the pattern.
Example
import java.util.regex.Pattern; public class PatternDemo < private static String REGEX = ":"; private static String INPUT = "hello:dear:guest"; public static void main(String[] args) < Pattern pattern = Pattern.compile(REGEX); String[] result = pattern.split(INPUT); for (String data: result) < System.out.println(data); >> >
This will produce the following result:
Result
Split string in java pattern
если не хотите экранировать или не знаете, надо ли, просто пишите text.split(Pattern.quote(«<нужный знак>«)); Тогда значение всегда будет строковым и не касаться регулярок. Ну и если нет автоимпорта, то не забудьте импортировать java.util.regex.Patternнужный>
Статья класс, можно добавить, что в качестве параметра split принимает и сразу несколько разделителей
String str = "Амо/ре.амо,ре"; String[] words = str.split("[/\\.,]"); // Амо ре амо ре
А если нет разделителей, но есть сплошная строка длиной N символов, и нужно разделить ее, допустим, на число M ? То как в этом случае быть?
Когда я в первый раз прочитал данную статью, ничего не понял и посчитал данную статью бесполезной. А на тот момент я изучал первый месяц. И сейчас, спустя еще 2 месяца я наконец то понял что тут написано) И могу теперь смело сказать что да, статья полезная.
А если в задаче разделитель вводится с клавиатуры, то как добавить\\ чтоб ошибки зарезервированного знака не было?
По-моему, зря ничего не сказано про то, что точка ( «.») не может служить разделителем, в отличие от запятой например. И её надо обозначать слэшами — («\\.»)
JavaRush — это интерактивный онлайн-курс по изучению Java-программирования c нуля. Он содержит 1200 практических задач с проверкой решения в один клик, необходимый минимум теории по основам Java и мотивирующие фишки, которые помогут пройти курс до конца: игры, опросы, интересные проекты и статьи об эффективном обучении и карьере Java‑девелопера.
Этот веб-сайт использует данные cookie, чтобы настроить персонально под вас работу сервиса. Используя веб-сайт, вы даете согласие на применение данных cookie. Больше подробностей — в нашем Пользовательском соглашении.