The String.endsWith()
method in Java is used to check if a string ends with a specified suffix.
Table of Contents
- Introduction
endsWith
Method Syntax- Examples
- Basic Usage
- Case Sensitivity
- Using
endsWith
with Special Characters - Real-World Use Case
- Conclusion
Introduction
The String.endsWith()
method is a member of the String
class in Java. It allows you to determine if a string ends with a specified sequence of characters (suffix). This is particularly useful for validation and filtering purposes.
endsWith() Method Syntax
The syntax for the endsWith
method is as follows:
public boolean endsWith(String suffix)
- suffix: The suffix to check.
Examples
Basic Usage
The endsWith
method can be used to check if a string ends with a specified suffix.
Example
public class EndsWithExample {
public static void main(String[] args) {
String str = "Welcome to Java";
boolean result = str.endsWith("Java");
System.out.println("Ends with 'Java': " + result);
}
}
Output:
Ends with 'Java': true
Case Sensitivity
The endsWith
method is case-sensitive, meaning it will only return true if the exact suffix, including case, is found.
Example
public class CaseSensitiveExample {
public static void main(String[] args) {
String str = "Welcome to Java";
boolean result1 = str.endsWith("java");
boolean result2 = str.endsWith("Java");
System.out.println("Ends with 'java': " + result1);
System.out.println("Ends with 'Java': " + result2);
}
}
Output:
Ends with 'java': false
Ends with 'Java': true
Using endsWith
with Special Characters
The endsWith
method can also be used with strings containing special characters.
Example
public class SpecialCharactersExample {
public static void main(String[] args) {
String str = "Welcome to Java @2021!";
boolean result = str.endsWith("@2021!");
System.out.println("Ends with '@2021!': " + result);
}
}
Output:
Ends with '@2021!': true
Real-World Use Case
Example: Checking File Extensions
One common use case for endsWith
is checking file extensions, such as validating if a file is an image based on its extension.
public class FileExtensionExample {
public static void main(String[] args) {
String fileName1 = "photo.jpg";
String fileName2 = "document.pdf";
if (fileName1.endsWith(".jpg") || fileName1.endsWith(".png")) {
System.out.println(fileName1 + " is an image file.");
} else {
System.out.println(fileName1 + " is not an image file.");
}
if (fileName2.endsWith(".jpg") || fileName2.endsWith(".png")) {
System.out.println(fileName2 + " is an image file.");
} else {
System.out.println(fileName2 + " is not an image file.");
}
}
}
Output:
photo.jpg is an image file.
document.pdf is not an image file.
In this example, the endsWith
method is used to check if the filenames have the correct image file extensions (e.g., .jpg or .png).
Conclusion
The String.endsWith()
method in Java is a simple yet powerful tool for checking if a string ends with a specified suffix. It is case-sensitive and works with strings containing special characters. This method is particularly useful for validation and filtering strings in various applications. By understanding and utilizing the endsWith
method, you can efficiently manage string suffix checks in your Java programs.