String Class Methods in Java: Session 2

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 basically 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. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="There are 365 days in a year";  
  6.       System.out.println("Does string starts with 'are'..?? "+str1.startsWith("are"));//It returns False  
  7.       System.out.println("Does string starts with 'There'..?? "+str1.startsWith("There"));//It returns True        System.out.println("Does substring of str1 (starting from 4th index) has 'are' prefix..?? "+str1.startsWith("are",4)); //It returns False  
  8.       System.out.println("Does substring of str1 (starting from 6th index) has 'are' prefix..?? "+str1.startsWith("are",6)); //It returns True  
  9.    }  
  10. }  

Output



In the preceding example we can see that this method can easily check whether the specified string is a prefix of the specific 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 or 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. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="Sunday comes after saturday";  
  6.       System.out.println("Does string ends with 'comes'..?? "+str1.endsWith("comes"));//It returns False  
  7.       System.out.println("Does string ends with 'saturday'..?? "+str1.endsWith("saturday"));//It returns True  
  8.    }  
  9. }  

Output



Method: Java String charAt()

This method is used to return the character at a specified index value. The index value should start form 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.  {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="Dead or alive";  
  6.       char ch1=str1.charAt(1);  
  7.       char ch2=str1.charAt(4);  
  8.       char ch3=str1.charAt(3);  
  9.       char ch4=str1.charAt(7);  
  10.       char ch5=str1.charAt(12);  
  11.       System.out.println("1st index character is: "+ch1);  
  12.       System.out.println("4th index character is: "+ch2);  
  13.       System.out.println("3rd index character is: "+ch3);  
  14.       System.out.println("7th index character is: "+ch4);  
  15.       System.out.println("12th index character is: "+ch5);  
  16.    }  
  17. }  
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 length to count the number of characters per string.

  1. public class CharAt   
  2. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="What";  
  6.       String str2="Beat them";  
  7.       String str3="Dead or alive";  
  8.       String str4="Today is a very hot day.";  
  9.       System.out.println("The no. of characters in str1 is: "+str1.length());  
  10.       System.out.println("The no. of characters in str2 is: "+str2.length());  
  11.       System.out.println("The no. of characters in str3 is: "+str3.length());  
  12.       System.out.println("The no. of characters in str4 is: "+str4.length());  
  13.    }  
  14. }  

Output



Being curious to learn more...??? Click the link below.

Thank you, keep learning and sharing.

Up Next
    Ebook Download
    View all
    Learn
    View all