String Class Methods in Java: Session 7

The preceding articles introduced a few more string methods used in Java by showing classic examples for a better explanation.

Method: Java String split()

 
This method is usually used to split a string into substrings based on the given regular expression or delimiter. It returns the tokens in the form of a char array. It throws PatternSyntaxException() if the pattern or syntax for the specified regular expression is not valid. It has the following two variants:
  • Java String split(String regex)
  • Java String split(String regex, int limit)
 
The split(String regex) method returns an array of strings after splitting the specified input string based on the delimiting regular expression.
 
The split(String regex, int limit) method limits the number of strings returned after splitting. If the limit is negative then the returned array would have as many substrings as possible in the specific string and if the limit is zero then the returned array would have all the substrings, excluding the trailing empty strings (matching regex). For example in split(“delimiter”, 4) the limit is 4, so the returned array would have only 4 substrings.
 
An example will make everything clear about both variants.
 

Example: split(String regex)

 
Here in this example, the splitting is done based on the three delimiters.
  1. public class SplitVariant1   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         String str1 = "Have a nice day";  
  6.         String str2 = "354-852-255";  
  7.         String str3 = "DD/MM/YYYY";  
  8.         System.out.println("Outputs for split(String regex): ");  
  9.         System.out.println();  
  10.         System.out.println("Returned substrings of str1 are: ");  
  11.         String arr1[] = str1.split("\\s");  
  12.         for (String substr1: arr1)   
  13.         {  
  14.             System.out.println(substr1);  
  15.         }  
  16.         System.out.println("Returned substrings of str2 are: ");  
  17.         String arr2[] = str2.split("-");  
  18.         for (String substr2: arr2)   
  19.         {  
  20.             System.out.println(substr2);  
  21.         }  
  22.         System.out.println("Returned substrings of str3 are: ");  
  23.         String arr3[] = str3.split("/");  
  24.         for (String substr3: arr3)   
  25.         {  
  26.             System.out.println(substr3);  
  27.         }  
  28.     }  
  29. }  
Output

Example: split(String regex, int limit)

 
Here in this example, various limits are being used for splitting.
  1. public class SplitVariant2   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         String str1 = "Have a nice day";  
  6.         String str2 = "354-852-255";  
  7.         String str3 = "DD/MM/YYYY";  
  8.         System.out.println("Outputs for split(String regex, int limit): ");  
  9.         System.out.println();  
  10.         System.out.println("Returned substrings of str1 with limit=2 are: ");  
  11.         String arr[] = str1.split("\\s"2);  
  12.         for (String substr: arr)   
  13.         {  
  14.             System.out.println(substr);  
  15.         }  
  16.         System.out.println("Returned substrings of str1 with limit=3 are: ");  
  17.         String arr1[] = str1.split("\\s"3);  
  18.         for (String substr1: arr1)   
  19.         {  
  20.             System.out.println(substr1);  
  21.         }  
  22.         System.out.println("Returned substrings of str2 with limit=2 are: ");  
  23.         String arr2[] = str2.split("-"2);  
  24.         for (String substr2: arr2)   
  25.         {  
  26.             System.out.println(substr2);  
  27.         }  
  28.         System.out.println("Returned substrings of str3 with limit=2 are: ");  
  29.         String arr3[] = str3.split("/"2);  
  30.         for (String substr3: arr3)   
  31.         {  
  32.             System.out.println(substr3);  
  33.         }  
  34.         System.out.println("Returned substrings of str2 with limit=0 are: ");  
  35.         String arr4[] = str2.split("-"0);  
  36.         for (String substr4: arr4)   
  37.         {  
  38.             System.out.println(substr4);  
  39.         }  
  40.         System.out.println("Returned substrings of str3 with limit=0 are: ");  
  41.         String arr5[] = str3.split("/"0);  
  42.         for   
  43.         {  
  44.             System.out.println(substr5);  
  45.         }  
  46.         System.out.println("Returned substrings of str2 with limit=-3 are: ");  
  47.         String arr6[] = str2.split("-", -3);  
  48.         for (String substr6: arr6)   
  49.         {  
  50.             System.out.println(substr6);  
  51.         }  
  52.         System.out.println("Returned substrings of str3 with limit=-5 are: ");  
  53.         String arr7[] = str3.split("/", -5);  
  54.         for (String substr7: arr7)  
  55.         {  
  56.             System.out.println(substr7);  
  57.         }  
  58.     }  
  59. }  
Output
 

We can observe that with the limit zero (0) and negative (-ve) the results are the same however in some cases the result would be different. Let's see with another short example.
 
Example
  1. public class SplitVariant2   
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         String str1 = "112233112222";  
  6.         System.out.println("Outputs for split(String regex, int limit): ");  
  7.         System.out.println();  
  8.         System.out.println("Returned substrings of str1 with limit=-3 are: ");  
  9.         String arr[] = str1.split("2", -3);  
  10.         for (String substr: arr)  
  11.         {  
  12.             System.out.println(substr);  
  13.         }  
  14.         System.out.println("Returned substrings of str1 with limit=0 are: ");  
  15.         String arr2[] = str1.split("2"0);  
  16.         for (String substr2: arr2)  
  17.         {  
  18.             System.out.println(substr2);  
  19.         }  
  20.         System.out.println("Returned substrings of str1 with limit=-5 are: ");  
  21.         String arr1[] = str1.split("2", -5);  
  22.         for (String substr1: arr1)   
  23.         {  
  24.             System.out.println(substr1);  
  25.         }  
  26.     }  
  27. }  
Output
 

Now in the preceding output, arr and arr1 have the results {“11”,”3311”, ”" , ”" } for a negative limit but arr2 has the result {“11”,”3311”} because the limit zero excludes trailing empty strings, in other words, “2”.

Method: Java String isEmpty()

 
This method checks whether or not the given string is empty. It basically checks the length of the string, if there is some length then it returns false and if the length is zero then it returns true. It throws NullPointerException() if the string is null. This method is included in Java since version 1.6. There are various ways to check the emptiness of the string, like isEmpty(), isBlank(), length() and so on. Let' move to an example for better understanding.
Example
  1. public class IsEmpty  
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         String str1 = "Hello";  
  6.         String str2 = "Do you know me..??";  
  7.         String str3 = "";  
  8.         String str4 = " ";  
  9.         String str5 = null;  
  10.         System.out.println("Is str1 empty..? " + str1.isEmpty()); //returns false  
  11.         System.out.println("Is str2 empty..? " + str2.isEmpty()); //returns false  
  12.         System.out.println("Is str3 empty..? " + str3.isEmpty()); //returns true  
  13.         System.out.println("Is str4 empty..? " + str4.isEmpty()); //returns false because str4 has some length due to whitespace  
  14.         System.out.println("Is str5 empty..? " + str5.isEmpty()); //throws NullPointerException  
  15.     }  
  16. }  
Output

In the preceding output, we can observe that for the 4th print throws an exception as said above.

Methods: Java String join()

 
This method is used in joining two or more strings with a delimiter that is copied for each element in the string. A delimiter is a character value to be added with each element and it can also be “null”, provided that we need to pass “null” as the delimiter otherwise it will throw a NullPointerException() exception if the element or delimiter is “null”.
This method is included in Java since version 1.8. It has the following two variants:
  • Java String join(CharSequence delimiter, CharSequence….elements)
  • Java String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

Both variants return a new string composed of copies of character sequence elements joined together with a copy of the specified delimiter. The only difference is the syntax format. Let's see the examples for both variants.
Example
  1. import static java.lang.String.join;  
  2. import java.io.IOException;  
  3. import java.util.Arrays;  
  4. import java.util.List;  
  5.   
  6. public class JoinVariants   
  7. {  
  8.     public static void main(String args[]) throws IOException   
  9.     {  
  10.         String[] strAr = {"p""r""o""g""r""a""m"};  
  11.         String str1;  
  12.         str1 = join(",""Animal""Vegetables""Electronics");  
  13.         String str2;  
  14.         str2 = join("-""873""354""165");  
  15.         System.out.println("Output for 'join(CharSequence delimiter, CharSequence….elements)':");  
  16.         System.out.println();  
  17.         System.out.println("Returned string is: " + str1);  
  18.         System.out.println("Returned string is: " + str2);  
  19.         System.out.print("Returned string is: ");  
  20.         System.out.println(String.Utils.join(strAr));  
  21.         System.out.print("Returned string is: ");  
  22.         System.out.println(String.Utils.join(strAr, " "));  
  23.         System.out.println();  
  24.         System.out.println("Output for 'join(CharSequence delimiter, Iterable<? extends CharSequence> elements)':");  
  25.         System.out.println();  
  26.         List < String > strList = Arrays.asList(strAr);  
  27.         System.out.print("Returned string is: ");  
  28.         System.out.println(String.Utils.join(strAr, null));  
  29.         System.out.print("Returned string is: ");  
  30.         System.out.println(String.Utils.join(strAr, " "));  
  31.         System.out.print("Returned string is: ");  
  32.         System.out.println(String.Utils.join(strAr, "-"));  
  33.         String str;  
  34.         List < String > myList;  
  35.         myList = new ArrayList < > ();  
  36.         myList.add("one");  
  37.         myList.add("two");  
  38.         myList.add("three");  
  39.         str = join("-", myList);  
  40.         System.out.println("str = " + str);  
  41.     }  
  42. }  
Output
 

Click the link below to proceed further.
 
 
Thank you, keep learning and sharing.