Conteúdo do Curso
Java Extended
Java Extended
Method split()
How to split String into String[]
Let's say we have a task to split a sentence into an array of words. In this case, we can either append
until we encounter a space character (" "
), or we can use the split()
method.
The split()
method is a String
method that takes a regex
as a parameter. What is regex
? Regex
stands for regular expression, which is essentially a character or set of characters that we use to split our string. We can input a space character " "
in the regex, and then our sentence will be divided into words.
Let's take a look at an example:
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String string = "This is the sentence that we want to split"; String[] array = string.split(" "); printArray(array); } // do not change the code above // do not change code from below static void printArray(String[] array) { for (String element: array) { System.out.println(element + " "); } } // do not change the code above }
Here we have split the string into an array of words. We used " "
to indicate to the program that it should split the sentence at each occurrence of " "
.
How to split a String by a specific character
We can also split our string using any other character. For example, let's split the link at the occurrence of the character "/"
.
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String link = "codefinity.com/profile/my-home"; String[] array = link.split("/"); printArray(array); } // do not change the code above // do not change code from below static void printArray(String[] array) { for (String element: array) { System.out.println(element + " "); } } // do not change the code above }
We have split the URL into separate parts at each occurrence of the "/"
character, and now we can see each element of the URL. In other words, when we split the string at the "/"
character, we obtained an array of strings.
You may also notice that the method removes the specified character from the resulting array when splitting.
How to split a String into characters with String type
To split a string into an array at each occurrence of a single character, we can simply use empty quotation marks ""
as the parameter. Let's take a look at an example:
Main
package com.example; public class Main { // do not change code from below public static void main(String[] args) { String string = "c<>definity"; String[] array = string.split(""); printArray(array); } // do not change the code above // do not change code from below static void printArray(String[] array) { for (String element: array) { System.out.print(element + " "); } System.out.println(); } // do not change the code above }
We have split the string into individual letters. You might think that there is a specific method for this, like toCharArray()
, but the difference is that when using the split()
method, we obtain an array of elements of type String
, whereas with toCharArray()
, we directly obtain an array of elements of type char
.
Obrigado pelo seu feedback!