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.
- public class SplitVariant1
- {
- public static void main(String args[])
- {
- String str1 = "Have a nice day";
- String str2 = "354-852-255";
- String str3 = "DD/MM/YYYY";
- System.out.println("Outputs for split(String regex): ");
- System.out.println();
- System.out.println("Returned substrings of str1 are: ");
- String arr1[] = str1.split("\\s");
- for (String substr1: arr1)
- {
- System.out.println(substr1);
- }
- System.out.println("Returned substrings of str2 are: ");
- String arr2[] = str2.split("-");
- for (String substr2: arr2)
- {
- System.out.println(substr2);
- }
- System.out.println("Returned substrings of str3 are: ");
- String arr3[] = str3.split("/");
- for (String substr3: arr3)
- {
- System.out.println(substr3);
- }
- }
- }
Output
![]()
Example: split(String regex, int limit)
Here in this example, various limits are being used for splitting.
- public class SplitVariant2
- {
- public static void main(String args[])
- {
- String str1 = "Have a nice day";
- String str2 = "354-852-255";
- String str3 = "DD/MM/YYYY";
- System.out.println("Outputs for split(String regex, int limit): ");
- System.out.println();
- System.out.println("Returned substrings of str1 with limit=2 are: ");
- String arr[] = str1.split("\\s", 2);
- for (String substr: arr)
- {
- System.out.println(substr);
- }
- System.out.println("Returned substrings of str1 with limit=3 are: ");
- String arr1[] = str1.split("\\s", 3);
- for (String substr1: arr1)
- {
- System.out.println(substr1);
- }
- System.out.println("Returned substrings of str2 with limit=2 are: ");
- String arr2[] = str2.split("-", 2);
- for (String substr2: arr2)
- {
- System.out.println(substr2);
- }
- System.out.println("Returned substrings of str3 with limit=2 are: ");
- String arr3[] = str3.split("/", 2);
- for (String substr3: arr3)
- {
- System.out.println(substr3);
- }
- System.out.println("Returned substrings of str2 with limit=0 are: ");
- String arr4[] = str2.split("-", 0);
- for (String substr4: arr4)
- {
- System.out.println(substr4);
- }
- System.out.println("Returned substrings of str3 with limit=0 are: ");
- String arr5[] = str3.split("/", 0);
- for
- {
- System.out.println(substr5);
- }
- System.out.println("Returned substrings of str2 with limit=-3 are: ");
- String arr6[] = str2.split("-", -3);
- for (String substr6: arr6)
- {
- System.out.println(substr6);
- }
- System.out.println("Returned substrings of str3 with limit=-5 are: ");
- String arr7[] = str3.split("/", -5);
- for (String substr7: arr7)
- {
- System.out.println(substr7);
- }
- }
- }
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
- public class SplitVariant2
- {
- public static void main(String args[])
- {
- String str1 = "112233112222";
- System.out.println("Outputs for split(String regex, int limit): ");
- System.out.println();
- System.out.println("Returned substrings of str1 with limit=-3 are: ");
- String arr[] = str1.split("2", -3);
- for (String substr: arr)
- {
- System.out.println(substr);
- }
- System.out.println("Returned substrings of str1 with limit=0 are: ");
- String arr2[] = str1.split("2", 0);
- for (String substr2: arr2)
- {
- System.out.println(substr2);
- }
- System.out.println("Returned substrings of str1 with limit=-5 are: ");
- String arr1[] = str1.split("2", -5);
- for (String substr1: arr1)
- {
- System.out.println(substr1);
- }
- }
- }
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 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
- public class IsEmpty
- {
- public static void main(String args[])
- {
- String str1 = "Hello";
- String str2 = "Do you know me..??";
- String str3 = "";
- String str4 = " ";
- String str5 = null;
- System.out.println("Is str1 empty..? " + str1.isEmpty());
- System.out.println("Is str2 empty..? " + str2.isEmpty());
- System.out.println("Is str3 empty..? " + str3.isEmpty());
- System.out.println("Is str4 empty..? " + str4.isEmpty());
- System.out.println("Is str5 empty..? " + str5.isEmpty());
- }
- }
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 an 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
- import static java.lang.String.join;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.List;
-
- public class JoinVariants
- {
- public static void main(String args[]) throws IOException
- {
- String[] strAr = {"p", "r", "o", "g", "r", "a", "m"};
- String str1;
- str1 = join(",", "Animal", "Vegetables", "Electronics");
- String str2;
- str2 = join("-", "873", "354", "165");
- System.out.println("Output for 'join(CharSequence delimiter, CharSequence….elements)':");
- System.out.println();
- System.out.println("Returned string is: " + str1);
- System.out.println("Returned string is: " + str2);
- System.out.print("Returned string is: ");
- System.out.println(String.Utils.join(strAr));
- System.out.print("Returned string is: ");
- System.out.println(String.Utils.join(strAr, " "));
- System.out.println();
- System.out.println("Output for 'join(CharSequence delimiter, Iterable<? extends CharSequence> elements)':");
- System.out.println();
- List < String > strList = Arrays.asList(strAr);
- System.out.print("Returned string is: ");
- System.out.println(String.Utils.join(strAr, null));
- System.out.print("Returned string is: ");
- System.out.println(String.Utils.join(strAr, " "));
- System.out.print("Returned string is: ");
- System.out.println(String.Utils.join(strAr, "-"));
- String str;
- List < String > myList;
- myList = new ArrayList < > ();
- myList.add("one");
- myList.add("two");
- myList.add("three");
- str = join("-", myList);
- System.out.println("str = " + str);
- }
- }
Output
![]()
Click the link below to proceed further.
Thank you, keep learning and sharing.