Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
String methods - split() - Testingpool

String methods – split()

In this post, we will discuss about the string methods – Split().

This method is used to split the string on the basis of given regular expression in arguments. It will return a String array.

  1. String[] split(String regex)
  2. String[] split(String regex,int limit)

String[] split(String regex):

This will split the String on the basis of given regular expression. Let’s look at the example below. Where we are splitting an Ip address on the basis of dot(.) and another string is splitted with space(” “).

public class ExampSplit {
	public static void main(String[] args) {
		
		String ip = "124.100.125.10";
		String[] ipArr = ip.split("\\.");     
		for(int i=0; i<ipArr.length; i++){
			System.out.println("arr["+i+"] : "+ipArr[i]);
		}
	System.out.println("**********************************");
		// another example 
		String val1 = "This is testingpool.com";
		String[] arr = val1.split(" ");
		for(String value : arr){
			System.out.println(value);
		}
	}
}
Output:
arr[0] : 124
arr[1] : 100
arr[2] : 125
arr[3] : 10
**********************************
This
is
testingpool.com

String[] split(String regex,int limit):

This method has one more parameter called ‘limit’, which decides the length of array returned after splitting the String.Let’s understand with the example below.

public class ExampSplit {
	public static void main(String[] args) {
		
		String ip = "124.100.125.10";
		
		String[] arr1 =ip.split("\\.", 2); 
		String[] arr2 =ip.split("\\.", 3); 
		System.out.println("Length of first array : "+arr1.length);
		System.out.println("Length of Second array : "+arr2.length);
		// First array
		for(String a1  : arr1){
			System.out.println(a1);
		}
		
		System.out.println("************************");
		// Second array
		for(String a2  : arr2){
			System.out.println(a2);
		}
	}
}
Output:
Length of first array : 2
Length of Second array : 3
124
100.125.10
************************
124
100
125.10
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...