Course Content
Java Extended
Java Extended
Method split()
The split(String delimiter)
method splits a string into an array of substrings based on the specified delimiter (a character or sequence of characters). It returns an array where each element is a substring from the original string, separated by the delimiter.
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; //do not modify the code below this comment public class Main { public static void main(String[] args) { String string = "This is the sentence that we want to split"; // split the string into an array based on spaces String[] array = string.split(" "); printArray(array); } // method to print each element of the array static void printArray(String[] array) { for (String element: array) { System.out.println(element + " "); } } }
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; // do not modify the code below this comment public class Main { public static void main(String[] args) { String link = "codefinity.com/profile/my-home"; // split the string into an array based on "/" String[] array = link.split("/"); printArray(array); // print the array elements } // method to print each element of the array static void printArray(String[] array) { for (String element: array) { System.out.println(element + " "); } } }
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; // do not modify the code below this comment public class Main { public static void main(String[] args) { String string = "c<>definity"; // split the string into an array of individual characters String[] array = string.split(""); printArray(array); } // method to print each element of the array static void printArray(String[] array) { for (String element: array) { System.out.print(element + " "); } // print a new line after the array elements System.out.println(); } }
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
.
Thanks for your feedback!