AppDividend
Latest Code Tutorials

Java String startsWith() Function Example Tutorial

0

Java string startsWith() is an inbuilt method of String class that is used for checking the prefix of the String. The startsWith() method can either start checking from the first index or can start checking from a specified index. It returns the boolean value true or false based on whether the given string begins with the specified letter or word.

At times it is required to check whether a particular string starts with a given prefix or not. Java takes care of this through its java.lang.String.startsWith() method.  This leads to two variants of the method:

  1. public boolean startsWith(String prefix)
  2. public boolean startsWith(String prefix, int index)

Java String startsWith()

Java String startsWith() method is used to check the prefix of string.

It verifies if the given string starts with an argument string or not. Java String startsWith() method is overloaded method and has two forms:

boolean startsWith(String str):

It returns true if the str is a prefix of the String.

boolean startsWith(String str, int fromIndex):

It returns true if the String begins with str starting from the specified index fromIndex. For example, let’s say that the value of a String s is “Hi there” and we are calling starsWith() method like this: s.startsWith(“there”, 3) then this will return true because we have passed value 3 as fromIndex, the searching of keyword “there” begins from an index 3 of the given string s. It is found at the beginning of a string s.

Java String startsWith() Function Example

Internal Implementation

public boolean startsWith(String prefix, int toffset) {  
     char ta[] = value;  
     int to = toffset;  
     char pa[] = prefix.value;  
     int po = 0;  
     int pc = prefix.value.length;  
     // Note: toffset might be near -1>>>1.  
     if ((toffset < 0) || (toffset > value.length - pc)) {  
         return false;  
     }  
     while (--pc >= 0) {  
         if (ta[to++] != pa[po++]) {  
             return false;  
         }  
     }  
     return true;
 }

Starting at the first index

Syntax: public boolean startsWith(String prefix)

Parameters: The String that needs to be found at the beginning.

Returns: True if match found, otherwise returns False.

Example1.java:

The following example demonstrates the use of the startswith() method beginning the match from the first index.

See the following code.

public class Example1 {
  public static void main(String[] args) {
    String s1 = new String("This is Java.");
    System.out.println(s1);

    System.out.println(s1.startsWith("This"));
  }
}

Output

Starting at the first index

Example2.java:

The following example demonstrates a similar situation.

public class Example2 {
  public static void main(String[] args) {
    String s1 = new String("This is Java.");
    System.out.println(s1);

    System.out.println(s1.startsWith("Th"));
  }
}

Output

Java String startsWith()

Example3.java:

The following example demonstrates a failed match.

public class Example3 {
  public static void main(String[] args) {
    String s1 = new String("This is Java.");
    System.out.println(s1);

    System.out.println(s1.startsWith("Java"));
  }
}

Output

Java String startsWith

Starting at any given index

Syntax: public boolean startsWith(String prefix, int index)

Parameters: The String that needs to be found and the index from which to start matching from

Returns: true if match found, otherwise returns false.

Example4.java:

The following example demonstrates the use of the startsWith() function to start from any given index:

See the following program.

public class Example4 {
  public static void main(String[] args) {
    String s1 = new String("This is Java.");
    System.out.println(s1);
    System.out.println(s1.startsWith("This", 8));
  }
}

Output

The startsWith()

Example5.java:

The following example demonstrates a failed match in this variation:

public class Example5 {
  public static void main(String[] args) {
    String s1 = new String("This is Java.");
    System.out.println(s1);

    System.out.println(s1.startsWith("Java", 8));
  }
}

Output

Java string example

This is how the java.lang.String.startsWith() method can be used to match a string with a given prefix either starting from the first index or any given index.

Conclusion

The method startsWith() is a convenience method that checks whether a string starts with another String. We can also pass the index of the first character to start checking from.

Finally, Java String startsWith() Function Example is over.

Related Posts

Java String split() Function

Java String join() Method

Java String Intern() Function

Java String replace() Function

Java String replaceAll() Function

Leave A Reply

Your email address will not be published.

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