String

String endsWith method

With this example we are going to demonstrate how to use the endsWith method of String. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. In short, to check if a String ends with a specified suffix you should:

  • Create a new String.
  • Use endsWith(String suffix) API method of String. This method tests if this string ends with the specified suffix.

Let’s take a look at the code snippet that follows:
 

package com.javacodegeeks.snippets.core;

public class StringEndsWithMethod {
	
	public static void main(String[] args) {
		
		String s = "Java Code Geeks";
		
		if (s.endsWith("Geeks")) {
			System.out.println("String ends with Geeks");
		}
		else {
			System.out.println("String does not end with Geeks");
		}
		
		if (s.endsWith("Unknown")) {
			System.out.println("String ends with Unknown");
		}
		else {
			System.out.println("String does not end with Unknown");
		}
		
	}

}

Output:

String ends with Geeks
String does not end with Unknown

 
This was an example of endsWith method of String in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button