Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, March 15, 2019

String endsWith​ Method in Java With Example - Internal Implementation

String endsWith​ method in java:

Java String endsWith​ method is used to check the string is ending with the specified string or not. This method returns true if the string is ending with given string or suffix.
Returns false if not ending with the specified string.


String endsWith​ method in java


Syntax:

public boolean endsWith​(String suffix)


Tests if this string ends with the specified suffix.

Parameters:

suffix - the suffix. This string will be searched in the input string.

Returns:

true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the equals(Object) method.

String endsWith​ Method Example in Java:

Learn how to write a program to use String class endsWith​ method to check the string ends with the our string. In the following example, 3 strings are checked with endsWith function.


public class StringEndswithExample {
 public static void main(String[] args) {
  String input1 = "java";
  String input2 = "w3schools";
  String input3 = "java-w3schools.blogspot.com";
  
  System.out.println("input1.endsWith(\"va\") :: "+input1.endsWith("va"));
  System.out.println("input2.endsWith(\"schools\") :: "+input2.endsWith("schools"));
  System.out.println("input2.endsWith(\"blogspot.com\") :: "+input3.endsWith("blogspot.com"));
 }
}

endsWith​ Method Example Output:

The above program produces the below output. 

input1.endsWith("va") :: true
input2.endsWith("schools") :: true
input2.endsWith("blogspot.com") :: true

Check if String is Valid Domain or Not in Java:

In this example, Learn how to write a java program to check whether the given string is a valid domain or not. We will check for the .com, .org and .net domain in our usecase.

package examples.java.w3schools.string;

public class EndswithDomainChecker {
 public static void main(String[] args) {
  String domain1 = "https://en.wikipedia.org";
  String domain2 = "java-w3schools.blogspot.com";
  String domain3 = "oracle.java";

  System.out.println("Checking Domain 1 : "+checkDomain(domain1));
  System.out.println("Checking Domain 2 : "+checkDomain(domain2));
  System.out.println("Checking Domain 3 : "+checkDomain(domain3));

 }

 private static String checkDomain(String domainName) {

  if (domainName.endsWith(".com")) {
   return "Valid domain " + domainName;
  } else if (domainName.endsWith(".org")) {
   return "Valid domain " + domainName;
  } else if (domainName.endsWith(".net")) {
   return "Valid domain " + domainName;
  }

  return "Invalid domain " + domainName;
 }
}



Check Domain Output:

The above program produces the below output. Implemented checkDomain method which takes domain as input and checks it is valid or invalid name for domain.

Checking Domain 1 : Valid domain https://en.wikipedia.org
Checking Domain 2 : Valid domain java-w3schools.blogspot.com
Checking Domain 3 : Invalid domain oracle.java

String endsWith​ Method Internal Implementation:

We will see now how String endsWith​ method works internally. Below is the internal code.

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


endsWith method calls internally startsWith method by passing specified string and input_string.length - specified_string.length.

For example: 

input = java-w3schools and input_string.length = 13
specified string: w3schools and specified_string.length = 8

startsWith("w3schools", 13-8); --> startsWith("w3schools", 5)

So, it searches in the input_string from position or index 5 to till end for the specified_string. If found then returns true, otherwise false.

Please post your questions in the comment section.

No comments:

Post a Comment

Please do not add any spam links in the comments section.