String Class Methods in Java: Session 8

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

Method: Java string substring()

 
This method is usually used to get a part or substring of a specifically given string. The substring starts or begins with the character at the specified index and extends to the end of the string. If we do not specify the endIndex, then this method will return all the characters from startIndex until the end of the string.
 
The startIndex is inclusive, in other words, starts from zero (0) and the endIndex is exclusive in other words starts from 1.
 
Note: It throws an exception, StringIndexOutOfBoundsException(), if the startIndex or beginIndex is less than zero (0) or greater than the length of the string or if the startIndex is a negative value or less than the starting index.
 
It has the following two variants:
  • Java String substring(int startIndex) Or substring(int beginIndex)
  • Java String substring(int startIndex, int endIndex) Or substring(int beginIndex, int endIndex)
The substring(int startIndex) returns the part or substring of the specific given the string starting from the specified index (startIndex) until the end of the string.
 
The substring(int startIndex, int endIndex) returns the substring of a given string, starting from the specified index (startIndex) and ending until specified index (endIndex).
 
Let's move to an example for clarification of this method.
 
Example
 
The following provides explanations for both variants.
  1. public class SubstringVariants     
  2. {    
  3.     public static void main(String args[])     
  4.     {    
  5.         String str1 = "Collection";    
  6.         String str2 = "Welcome to this session";    
  7.         String str3 = "687/6435-2004 507";    
  8.         System.out.println("Output for substring(int startIndex)");    
  9.         System.out.println();    
  10.         System.out.println("Substring of str1 is: " + str1.substring(3));    
  11.         System.out.println("Substring of str2 is: " + str2.substring(12));    
  12.         System.out.println("Substring of str3 is: " + str3.substring(5));    
  13.         System.out.println("Substring of str3 is: " + str3.substring(11));    
  14.         System.out.println();    
  15.         System.out.println("Output for substring(int startIndex, int endIndex)");    
  16.         System.out.println();    
  17.         System.out.println("Substring of str1 is: " + str1.substring(59));    
  18.         System.out.println("Substring of str2 is: " + str2.substring(820));    
  19.         System.out.println("Substring of str3 is: " + str3.substring(210));    
  20.         System.out.println("Substring of str3 is: " + str3.substring(715));    
  21.     }    
  22. }     
Output
 

Method: Java String toCharArray()

 
This method returns the newly created array of characters after converting the given string into a sequence of characters. The returned array length is equal to the length of the given string and the character sequence or content in the array matches the contents of the given string.
 
Example
  1. public class ToCharArray    
  2. {    
  3.     public static void main(String args[])    
  4.     {    
  5.         String str1 = "Acknowledgement";    
  6.         String str2 = "This is last session";    
  7.         String str3 = "ABCD1234-5678efgh/";    
  8.         char[] arr1 = str1.toCharArray();    
  9.         System.out.print("Returned char array of str1 is: ");    
  10.         for (char c1: arr1)    
  11.         {    
  12.             System.out.print(c1);    
  13.         }    
  14.         System.out.println();    
  15.         char[] arr2 = str2.toCharArray();    
  16.         System.out.print("Returned char array of str2 is: ");    
  17.         for (char c2: arr2)    
  18.         {    
  19.             System.out.print(c2);    
  20.         }    
  21.         System.out.println();    
  22.         char[] arr3 = str3.toCharArray();    
  23.         System.out.print("Returned char array of str3 is: ");    
  24.         for (char c3: arr3)     
  25.         {    
  26.             System.out.print(c3);    
  27.         }    
  28.         System.out.println();    
  29.     }    
  30. }     
Output

Method: Java String hashCode()

 
This method is used to determine the hash code for the given string. The hash code in Java is a 32-bit signed integer value that represents the state of the object upon which it is called and it is generated by the hash function. The hash code for integer "1" is "1" itself because an integer's hash code and its value are the same things. But the hash code for any character is equal to its ASCII character code.
 
The hash code for a string object can be computed by the following.
  1. s[0]*31^(n-1)+s[1]*31^(n-2)+…….+s[n-1]  
Let's see the example for this method.
 
Example
  1. public class HashCode    
  2. {    
  3.     public static void main(String args[])     
  4.     {    
  5.         String str1 = "Professional";    
  6.         String str2 = "Finally, all done";    
  7.         String str3 = "(ab12-34cd/ef56)";    
  8.         String str4 = "";    
  9.         String str5 = " ";    
  10.         System.out.println("Returned hash code of str1 is: " + str1.hashCode());    
  11.         System.out.println();    
  12.         System.out.println("Returned hash code of str2 is: " + str2.hashCode());    
  13.         System.out.println();    
  14.         System.out.println("Returned hash code of str3 is: " + str3.hashCode());    
  15.         System.out.println();    
  16.         System.out.println("Returned hash code of str4 is: " + str4.hashCode());    
  17.         System.out.println();    
  18.         System.out.println("Returned hash code of str5 is: " + str5.hashCode());    
  19.     }    
  20. }     
Output
hashcode
 
We can observe the different hash code for different strings, especially for no char string and string having the only whitespace.
 

Method: Java String matches()

 
This method basically helps to check or find whether or not the specified string matches the given regular expression. If it matches then it returns true otherwise false. The invocation of this method by the form str.matches(regex) yields a similar result as the form pattern.matches(regex, str). It throws PatternSyntaxException() if the specified regular expression is invalid. Let's move to an example.
  
Example
  1. public class Matches     
  2. {    
  3.     public static void main(String args[])     
  4.     {    
  5.         String str1 = "Welcome to the final match";    
  6.         String str2 = "Professional";    
  7.         String str3 = "ab12-34cd/ef56";    
  8.         System.out.println("Is regex 'final' matches with str1: " + str1.matches("(.*)final(.*)"));    
  9.         System.out.println("Is regex 'finale' matches with str1: " + str1.matches("(.*)finale(.*)")); //finale word is not in str1    
  10.         System.out.println("Is regex 'Welcome' matches with str1: " + str1.matches("Welcome(.*)"));    
  11.         System.out.println("Is regex 'match' matches with str1: " + str1.matches("(.*)match"));    
  12.         System.out.println("Is regex 'to the' matches with str1: " + str1.matches("to the")); // not mentioned complete regex    
  13.         System.out.println("Is regex 'PROF' matches with str2: " + str2.matches("PROF(.*)")); // alphabet case is not same    
  14.         System.out.println("Is regex 'fess' matches with str2: " + str2.matches("(.*)fess(.*)"));    
  15.         System.out.println("Is regex 'mess' matches with str2: " + str2.matches("mess"));    
  16.         System.out.println("Is regex '34c' matches with str3: " + str3.matches("(.*)34c(.*)"));    
  17.         System.out.println("Is regex 'ab12' matches with str3: " + str3.matches("ab12(.*)"));    
  18.         System.out.println("Is regex '7fk' matches with str3: " + str3.matches("7fk"));    
  19.     }    
  20. }    
Output
 

Method: Java String regionMatches()

 
By this method, we can compare the substring of the input string with the substring of the given string and can check whether the two strings or substrings are equal or not OR they matches or not. It has the following two variants:
  • Java String regionMatches(int toffset, String other, into offset, int len)
  • Java String regionMatches(boolean ignoreCase, int toffset, String other, into offset, int len)
Both variants are similar but with a single difference, that first variant is case sensitive whereas the second variant has the option to ignore or consider the case.
  • The parameter int toffset is the starting offset of a subregion in the string.
  • The parameter int ooffset is the starting offset of a subregion in the string argument.
  • The parameter String other is the string argument.
  • The parameter Int len is the number of characters to be compared.
  • The parameter ignoreCase means the method will ignore the case if the substring matches the string.
Now let's see the example.
 
Example
  1. public class RegionMatchesVariants     
  2. {    
  3.     public static void main(String args[])    
  4.     {    
  5.         String str1 = "The final session is here";    
  6.         String str2 = "final";    
  7.         String str3 = "FinaL";    
  8.         String str4 = "FINAL";    
  9.         String str5 = "session";    
  10.         String str6 = "there";    
  11.         System.out.println("Output for 1st variant:");    
  12.         System.out.println("Value for 1st match with str2: " + str1.regionMatches(4, str2, 05)); //true    
  13.         System.out.println("Value for 2nd match with str3: " + str1.regionMatches(4, str3, 05)); // false,case is not same    
  14.         System.out.println("Value for 3nd match with str4: " + str1.regionMatches(4, str4, 05)); // false,case is not same    
  15.         System.out.println("Value for 4rd match with str5: " + str1.regionMatches(12, str5, 24)); //true for 'ssion'    
  16.         System.out.println("Value for 5rd match with str6: " + str1.regionMatches(21, str6, 05)); //false for 'there'    
  17.         System.out.println();    
  18.         System.out.println("Output for 2nd variant:");    
  19.         System.out.println();    
  20.         System.out.println("Value for 6th match with str2: " + str1.regionMatches(true4, str2, 05)); //true    
  21.         System.out.println("Value for 7th match with str3: " + str1.regionMatches(true4, str3, 05)); //true,ignores the case sensitivity    
  22.         System.out.println("Value for 8th match with str4: " + str1.regionMatches(true4, str4, 05)); //true,ignores the case sensitivity    
  23.         System.out.println("Value for 9th match with str5: " + str1.regionMatches(false10, str5, 05)); // ignoreCase is false then also    
  24.         //it returns true because string matches    
  25.         System.out.println("Value for 10th match with str6: " + str1.regionMatches(true21, str6, 05)); // ignoreCase is true then also    
  26.         //it returns false because string does not matches    
  27.     }    
  28. }    
Output
Thank you for having the patience for all the sessions. I hope you all liked this series by learning all the string methods.
Thank you, keep learning and sharing.
 


Similar Articles