Java String endsWith() Method
In this article, we will look at an inbuilt method of the String class called the endsWith() method in Java programming language. Without beating around the bush, let’s directly jump into the topic by looking at the endsWith() method’s necessity, advantages, and syntax.
Finally, we will examine a couple of codes that illustrate using the endsWith() method. To understand and master the concept quickly, I recommend you practice each code in the article, try to make some modifications and experiment with the code to cover different scenarios.
What does the endsWith() method In Java do?
The endsWith() method is an inbuilt method in the JavaString class. The endsWith() method in the Java String class is used to ascertain if a string concludes with a specified suffix. This method essentially examines whether the provided string terminates with the designated suffix.
The endsWith() method returns true when the provided suffix matches the end of the string; otherwise, it returns false. For instance, if the string is “FirstCode” and the suffix is “e,” the function will return true. Conversely, if the string is “FirstCode” and the suffix is “Hello,” the function will return false.
The endsWith() method works very similar to the contains() method, except that instead of checking if the substring u specified is anywhere within the string, the endWith() method checks if the string you specified is a suffix of the main string.
What is the syntax of the endsWith() method in Java?
Before we take a look at some programs that involve the endsWith() method, let us take a look at the syntax of it:
String1.endsWith(String2)
The above syntax checks if the String2 is a suffix for String1 (If String1 ends with String2). If String1 does end with String2, the function returns true. Otherwise, it returns false.
Now that we know the theory of the endsWith() method of the String class in Java let’s implement this knowledge in programs and see some applications of this method.
class Main { public static void main(String args[]) { String String1 = "FirstCode"; String String2 = "Code"; String String3 = "First"; boolean result1 = String1.endsWith(String2); boolean result2 = String1.endsWith(String3); System.out.println(result1); System.out.println(result1); } }
Output:
True
False
Is the Java endsWith() method case-sensitive?
Yes, the endsWith() method is case-sensitive, meaning it checks for the exact case in which you specified the string.
Here is an example:
class Main { public static void main(String args[]) { String String1 = "FirstCode"; String String2 = "Code"; String String3 = "code"; boolean result1 = String1.endsWith(String2); boolean result2 = String1.endsWith(String3); System.out.println(result1); System.out.println(result2); } }
Output:
True
False
In the above example, the string “Code” matches the suffix, not the string “code, ” as the case differs.
Pairing the endsWith() method with control flow statements
As we have seen, the endsWith() method returns boolean values, which makes it incredibly easy to use in control flow statements. Here is one such example of using it along with an If else statement:
public class Main { public static void main(String[] args) { String str = "Welocme to FirstCode"; if(str.endsWith("FirstCode")) {System.out.println("Ends with FirstCode");} else System.out.println("Does not end with FirstCode"); } }
Output:
Ends with FirstCode
Can we check if a string ends with an empty string using the endsWith() method?
If an empty string is provided as a parameter to the endsWith() method, it will consistently yield true. This is because appending an empty string to any existing string does not alter the original string. To illustrate, consider the scenario where the ” Hello ” string remains unchanged when an empty string is appended.
public class Main { public static void main(String argvs[]) { String String1 = "FirstCode"; System.out.println(String1.endsWith("")); } }
Output:
True
Can we check if a string ends with a space using the endsWith() method?
Yes, we can. Unlike the empty string, space is a character, thus we can check if the main string ends with a space. Here is an example showing the same:
public class Main { public static void main(String argvs[]) { String String1 = "FirstCode"; String String2 = "FirstCode "; System.out.println(String1.endsWith(" ")); System.out.println(String2.endsWith(" ")); } }
Output:
False
True
In the above code, String1 “FirstCode” does not end with a space, but String2 “FirstCode ” does end with a space.
Null pointer exception while using the Java endsWith() method
While using the endsWith() method, the compiler raises the NullPointerException if you pass “null” as the method’s parameter.
Here is an example:
public class Main { public static void main(String argvs[]) { String str = "FirstCode"; if(str.endsWith(null)) {System.out.println("suffic found");} else{System.out.println("Not found");} } }
Output:
Conclusion
In conclusion, the endsWith() method in Java’s String class is a valuable tool for efficiently verifying if a string concludes with a specified suffix. This article explored its need, syntax, and practical applications, emphasizing its case-sensitive nature and seamless integration with control flow statements. Learning the endsWith() method allows Java programmers to manipulate and match patterns in Java programming, offering a straightforward solution for suffix validation.