Java split string example


 

Java split string example

This section illustrates you the use of split() method.

This section illustrates you the use of split() method.

Java split string example

This section illustrates you the use of split() method.

The String class has provide several methods for examining the individual characters, for extracting substrings,  for comparing strings, for searching strings, for converting string to upper case and lowercase etc. The method split() is one of them. As the name suggests, this method can split the string into the given format and will return the string in the form of string array. Actually, it is based on regex expression with some characters which have a special meaning in a regex expression.

Here is the code:

class StringSplitExample {
	public static void main(String[] args) {
		String st = "Hello_World";
		String str[] = st.split("_");
		for (int i = 0; i < str.length; i++) {
			System.out.println(str[i]);
		}
	}
}

Output:

Hello
World

Ads