Site icon TechVidvan

Java String endsWith() Method with Examples

The Java String endsWith() system is used to test if a string ends with a given suffix no longer. However, it returns factual. Differently, it returns false If the string ends with a given suffix. EndsWith() fashion internally uses the startsWith() system to find the end result.

Return Data Type of Java endsWith() Method

Return Data Type of endsWith() system False Character sequence supplied in “ suffix ” does not match the end sequence of the calling string True Character sequence supplied in “ suffix ” matches the end sequence of the calling string.

Java String endsWith() method

The Java String class endsWith() system tests if this string ends with a given suffix. It returns proper if this string ends with the given suffix; additional returns fake.

Hand

The syntax or hand of the EndsWith() system is as follows. Chapter.

public boolean endsWith( String suffix)

Parameter

suffix Sequence of character
Returns
true or false

Internal perpetration

public boolean endsWith( String suffix){
return 
startsWith(suffix,value.length-suffix.value.length);

The internal perpetration shows that the endWith() system is dependent on the startsWith() system of the String class.

Java String endsWith() Method

Example

public class EndsWith{
public static void main( String args()){
String s1 = " TechVidvan";
(s1.endsWith(" n"));
(s1.endsWith(" TechVidvan"));
}

Affair
true
true

Java String endsWith() Method

Example

public class EndsWith{
public static void main( String() args){
String str = " Drink to TechVidvan";
(str.endsWith(" com"));
if(str.endsWith(" TechVidvan")){
(" String ends with TechVidvan");
differentlySystem.out.println(" It doesn't end with Techvidvan");

Affair
false
String ends with TechVidvan

Java String endsWith() system

The EndsWith() system keeps track of the case perceptivity of the current character in the string. The posterior software suggests the equal.

Example

public class EndsWith
/ main system
public static void main( String argvs())


String str = " Drink to TechVidvan";


/ the result of the following display statement
/ shows endsWith() considers the case acuteness of
/ the characters of a string
(str.endsWith(" techvidvan"));// false because T and t are different
(str.endsWith(" Tech Vidvan"));// true because every character is same

Affair
false
True

Java String endsWith() system

When an empty string is surpassed in the parameter of the approach endsWith(), the fashion generally returns a true cost. The motive in the reverse of that’s that a string norway variations whilst we take an empty string to it.

Statement

String str = " TechVidvan""";
results in
str = " TechVidvan";
Therefore, we can say that any string in Java ends with an empty string("").
illustration
public class EndsWith


/ main system
public static void main( String argvs())


String str = " Drink to TechVidvan""";
/ prints true
(str.endsWith(""));
/ prints false as there's no white space after the string
(str.endsWith(""));

Affair
true
False

Conclusion

Java String endsWith() fashion assessments whether or not a string ends with a given suffix or no longer. However, the endsWith( String suffix) approach returns factual differently false If the string ends with a givensuffix.EndsWith( String suffix) fashion throws NullPointerException while the suffix string is null.

Exit mobile version