Java - String split() Method
The Java string split() method returns a string array containing elements obtained by splitting the given string around matches of the given regular expression.
Syntax
public String[] split(String regex)
Parameters
regex |
specify the delimiting regular expression. |
Return Value
Returns the array of strings computed by splitting the string around matches of the given regular expression.
Exception
Throws PatternSyntaxException, if the regular expression's syntax is invalid.
Example:
In the example below, split() method returns the string array computed after splitting the given string by specified regex expression.
import java.lang.*; public class MyClass { public static void main(String[] args) { String MyString = "Hello-Cello-Hullo-Hallo-Jello"; //split the string by "-" regex expression String[] Arr = MyString.split("-"); //print the content of the string array System.out.println("Arr contains: "); for(String str: Arr) System.out.println(str); } }
The output of the above code will be:
Arr contains: Hello Cello Hullo Hallo Jello
❮ Java.lang - String