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.
- public class StartsWith {
- public static void main(String args[]) {
- String str1 = "There are 365 days in a year";
- System.out.println("Does string starts with 'are'..?? " + str1.startsWith("are"));
- System.out.println("Does string starts with 'There'..?? " + str1.startsWith("There"));
- System.out.println("Does substring of str1 (starting from 6th index) has 'are' prefix..?? " + str1.startsWith("are", 6));
- }
- }
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.
- public class EndsWith {
- public static void main(String args[]) {
- String str1 = "Sunday comes after saturday";
- System.out.println("Does string ends with 'comes'..?? " + str1.endsWith("comes"));
- System.out.println("Does string ends with 'saturday'..?? " + str1.endsWith("saturday"));
- }
- }
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.
- public class CharAt {
- public static void main(String args[]) {
- String str1 = "Dead or alive";
- char ch1 = str1.charAt(1);
- char ch2 = str1.charAt(4);
- char ch3 = str1.charAt(3);
- char ch4 = str1.charAt(7);
- char ch5 = str1.charAt(12);
- System.out.println("1st index character is: " + ch1);
- System.out.println("4th index character is: " + ch2);
- System.out.println("3rd index character is: " + ch3);
- System.out.println("7th index character is: " + ch4);
- System.out.println("12th index character is: " + ch5);
- }
- }
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.
- public class CharAt {
- public static void main(String args[]) {
- String str1 = "What";
- String str2 = "Beat them";
- String str3 = "Dead or alive";
- String str4 = "Today is a very hot day.";
- System.out.println("The no. of characters in str1 is: " + str1.length());
- System.out.println("The no. of characters in str2 is: " + str2.length());
- System.out.println("The no. of characters in str3 is: " + str3.length());
- System.out.println("The no. of characters in str4 is: " + str4.length());
- }
- }
Output
Being curious to learn more...??? Click the link below.
Thank you, keep learning and sharing.