String Class Methods in Java: Session 2

Introduction 

 
Before reading this article, please go through my first part, of the series. 
Now let's discuss the further string methods.

Method: Java String startsWith()

 
This method is used to verify the prefix of a string.  If the specified string is a prefix, or starting string of the specific string, then on the basis of that it returns the Boolean true or false.
The starting word or string of a sentence or statement or line of words is known as a prefix.
It has the following two variants:
  • String startsWith(String str)
  • String startsWith(String str, index fromIndex)
The first one returns Boolean true, if the given string is a prefix of the string, otherwise Boolean false.
The second one returns true, if the string begins with the given string, it starts checking from the specified index “fromIndex”.  By this, it can also check the index number of the character of the string.
Now let's see an example.
 
Example
 
In this example, the outputs for both variants are shown.
  1. public class StartsWith {  
  2.  public static void main(String args[]) {  
  3.   String str1 = "There are 365 days in a year";  
  4.   System.out.println("Does string starts with 'are'..?? " + str1.startsWith("are"));  
  5.   System.out.println("Does string starts with 'There'..?? " + str1.startsWith("There"));  
  6.   System.out.println("Does substring of str1 (starting from 6th index) has 'are' prefix..?? " + str1.startsWith("are"6));  
  7.  }  
  8. }  
Output
 
In the preceding example, we can see that this method can easily check whether the specified string is a prefix of the specified string, or not.
 
Note: The substring that has the 4th index number is "There", but in the method, (“are”,4) is written and that's why the method returns the Boolean false.  And, the substring that has the 6th index number is "are" and that's why it returns true.
 

Method: Java String endsWith()

 
This method checks the specified string for a suffix for the string.  If the given string lies at the end of the specific string, then it returns Boolean true otherwise false.
 
The ending word or string of a sentence, statement, or line of words is known as the suffix.
 
Example
 
In this example, there is one string to check.  Whether the given string is a suffix, or not.
  1. public class EndsWith {  
  2.  public static void main(String args[]) {  
  3.   String str1 = "Sunday comes after saturday";  
  4.   System.out.println("Does string ends with 'comes'..?? " + str1.endsWith("comes"));  
  5.   System.out.println("Does string ends with 'saturday'..?? " + str1.endsWith("saturday"));  
  6.  }  
  7. }  
Output
 

Method: Java String charAt()

 
This method is used to return the character at a specified index value.  The index value should start from 0 (zero).
Note: If the index value is less than 0 (zero), or greater and equal to the length of the string, then it throws the exception IndexOutOfBoundsException.
 
Example
 
In this example, we will extract some characters from the given string, using this method.
  1. public class CharAt {  
  2.  public static void main(String args[]) {  
  3.   String str1 = "Dead or alive";  
  4.   char ch1 = str1.charAt(1);  
  5.   char ch2 = str1.charAt(4);  
  6.   char ch3 = str1.charAt(3);  
  7.   char ch4 = str1.charAt(7);  
  8.   char ch5 = str1.charAt(12);  
  9.   System.out.println("1st index character is: " + ch1);  
  10.   System.out.println("4th index character is: " + ch2);  
  11.   System.out.println("3rd index character is: " + ch3);  
  12.   System.out.println("7th index character is: " + ch4);  
  13.   System.out.println("12th index character is: " + ch5);  
  14.  }  
  15. }  
Output
 
In the preceding output, we can observe that a few index values do not have any character, since it has white space and that's why the 4th and 7th index values do not return any character, in the output.
 
For the preceding example, we can show the index values for the given string.
 
Index value character Index value character
[0] D [7] _(space)
[1] e [8] a
[2] a [9] l
[3] d [10] i
[4] _(space) [11] v
[5] o [12] e
[6] r    

 
Method: Java String length()

 
This method is helpful in finding the length of the string.  It counts the total number of characters (including white spaces) present in the string and returns the total sum.
In Java, the length of the string is equal to, or the same as, the total units of Unicode codes.
 
Example
 
In the following example, there are four strings of different lengths to count the number of characters per string.
  1. public class CharAt {  
  2.  public static void main(String args[]) {  
  3.   String str1 = "What";  
  4.   String str2 = "Beat them";  
  5.   String str3 = "Dead or alive";  
  6.   String str4 = "Today is a very hot day.";  
  7.   System.out.println("The no. of characters in str1 is: " + str1.length());  
  8.   System.out.println("The no. of characters in str2 is: " + str2.length());  
  9.   System.out.println("The no. of characters in str3 is: " + str3.length());  
  10.   System.out.println("The no. of characters in str4 is: " + str4.length());  
  11.  }  
  12. }  
Output
 
 
Being curious to learn more...??? Click the link below.
Thank you, keep learning and sharing.