AppDividend
Latest Code Tutorials

Java String endsWith() Function Example | Java String Methods

0

Java String endsWith() Function Example | Java String Methods is today’s topic. Java string endsWith() method checks whether the given string ends with a specified suffix or not. If it’s found that the string is ending with that specified suffix, then it will return true; otherwise, it will return false.

Java String endsWith() Function

Java String endsWith(String suffix) method checks whether the String ends with the specified suffix.

This method returns the boolean value true or false. If the particular suffix is found at the end of the string, then it returns true else it returns false.

See the syntax.

public boolean endsWith(String suffix)

Here in the signature, the suffix indicates the specified suffix part.

Return Value

False: Character sequence supplied in “suffix” does not match the end sequence of the given string.

True: Character sequence supplied in “suffix” matches the end sequence of the given string.

See the following code.

class endswith {
  public static void main(String args[]) {
    String str = "Hello Welcome to Appdividend";
    if (str.endsWith("Appdividend")) {
      System.out.println("The string ends with Appdividend");
    } else
      System.out.println("The string does not ends with Appdividend");
  }
}

See the output.

 

Java String endsWith() Function Example

See another example.

public class JavaExample {
  public static void main(String[] args) {
    String str_Sample = "Eleven is Millie Bobby Brown";
    // Check if ends with a particular sequence
    System.out.println("EndsWith character 'Brown': " + str_Sample.endsWith("Brown"));
    System.out.println("EndsWith character 'n': " + str_Sample.endsWith("n"));
    System.out.println("EndsWith character 'Millie': " + str_Sample.endsWith("Millie"));
  }
}

See the output.

➜  java javac JavaExample.java
➜  java java JavaExample
EndsWith character 'Brown': true
EndsWith character 'n': true
EndsWith character 'Millie': false
➜  java

The endsWith() function returns true if this string ends with the specified suffix.

String endsWith() method with if statement

Since the method endsWith(,) returns the boolean value, and it can be used in the If statement as the condition as shown in the following example.

public class JavaExample {
  public static void main(String[] args) {
    String str = "Java String tutorial";
    if (str.endsWith("tutorial")) {
      System.out.println("The Given String ends with tutorial");
    }
  }
}

See the output.

➜  java java JavaExample
The Given String ends with tutorial
➜  java

Finally, Java String endsWith() Function Example | Java String Methods is over.

Recommended Posts

String contains() method in Java

Java String Concat() Method

Java String charAt() Method

equalsIgnoreCase() In Java Example

Java String Class Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.