beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – String endsWith() Method example

By Chaitanya Singh | Filed Under: String handling

The method endsWith(String suffix) checks whether the String ends with a specified suffix. This method returns a boolean value true or false. If the specified suffix is found at the end of the string then it returns true else false.

public boolean endsWith(String suffix)

Example

In this example we have two strings and we are checking whether the strings are ending with the specified suffixes.

public class EndsWithExample{
   public static void main(String args[]){
       String str1 = new String("This is a test String");
       String str2 = new String("Test ABC");
       boolean var1 = str1.endsWith("String");
       boolean var2 = str1.endsWith("ABC");
       boolean var3 = str2.endsWith("String");
       boolean var4 = str2.endsWith("ABC");
       System.out.println("str1 ends with String: "+ var1);
       System.out.println("str1 ends with ABC: "+ var2);
       System.out.println("str2 ends with String: "+ var3);
       System.out.println("str2 ends with ABC: "+ var4);
   }
}

Output:

str1 ends with String: true
str1 ends with ABC: false
str2 ends with String: false
str2 ends with ABC: true

Enjoyed this post? Try these related posts

  1. Java String format() method explained with examples
  2. Java – String trim() and hashCode() Methods
  3. Java String intern() method explained with examples
  4. Java – String toLowerCase() and toUpperCase() Methods
  5. Java – String indexOf() Method example
  6. Java – String substring() Method example

Comments

  1. Rafael says

    January 23, 2016 at 12:56 PM

    Hello, I have question connected with this Example.
    Is there easy way to check whether String is ending with 5 digits (ex. “21311” “24444” “52211”), but we don’t know digits, in the way you are showing on example above? I need to use it in my filterin method to leave only numbers in String, using Java 8 stream.

    Greetings, Rafael.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap