In Java, the String “startsWith()” method is useful in situations where the programmer needs to locate a string value(s) starting from a specific character. Moreover, this method helps in sorting the complex data based on which the records can be analyzed efficiently. This analysis, as a result, assists in deciding whether to keep or delete the unwanted data (consuming unnecessary memory).

Contents Overview

What is the String “startsWith()” Method in Java?

The String “startsWith()” method checks if the given string starts with the specified character(s) or not and returns the Boolean values “true” or “false” based on that.

Syntax

public boolean startsWith(String pref)
public boolean startsWith(String pref, int offset)

Syntax Explanation

  • pref” represents the starting character/prefix to check for in the string.
  • offset” is an optional parameter that checks if the specified character or a set of characters starts from the given index in the string. 

Return Value

This method returns “true” if the string starts with the specified character(s).

Internal Implementation of the String “startsWith()” Method

public boolean startsWith(String pref, int offset) {
  char x[] = value;
  int tf = offset;
  char y[] = pref.value;
  int a = 0;
  int b = pref.value.length;
  // Note: offset can be near -1>>>1.
  if ((offset < 0) || (offset > value.length - b)) {
    return false;
  }
  while (--b >= 0) {
    if (x[tf++] != y[a++]) {
      return false;
    }
  }
  return true;
}

Explanation

This implementation compares the string with a prefix i.e., pref (argument) such that each character is compared and the corresponding boolean value is returned. Moreover, this implementation also considers the offset/index to get the starting index for comparison.

Important Consideration

The String “startsWith()” method throws the “NullPointerException” if the argument passed to this method is “null” as demonstrated below:

Example 1: Applying the String “startsWith(String pref)” Method in Java

This example uses this method to apply a check upon the string for the starting character and set of characters differently:

package jbArticles;
public class Stringstartswith {
public static void main(String args[]) {
  String a = "This is Harry";
  System.out.println("Given String -> "+a);
  System.out.println("Does the string start with the specified character? " +a.startsWith("T"));
  System.out.println("Does the string start with the specified character? " +a.startsWith("t"));
  System.out.println("Does the string start with the specified characters? " +a.startsWith("This"));
  System.out.println("Does the string start with the specified character? " +a.startsWith("i"));
}
}

Code Explanation

  • Initialize a string to check for the starting characters and log it on the console.
  • Now, apply the “startsWith()” method to check if the string starts with the given uppercase and lowercase characters “T” and “t”, respectively.
  • After that, apply the method again to check for the starting set of characters and a non-existing starting character in the string.

Output

From this output, the following points can be analyzed:

  • This method is case-sensitive as it returns “false” on the specified starting “t” character.
  • This method is also compatible with multiple sets of characters.

Example 2: Applying the String “startsWith(String pref, int offset)” Method in Java

In this example, the “startsWith()” method will be used to check if the string starts with a particular character with respect to the specified index in the string. It is such that the search for the starting character will be started from the given offset/index in the string: 

package jbArticles;
public class Stringstartswith {
public static void main(String args[]) {
  String a = "This is Harry";
  System.out.println("Given String -> "+a);
  System.out.println("Does the string start with the specified character? " +a.startsWith("T"));
  System.out.println("Does the string start with the specified character? " +a.startsWith("q", 3));
  System.out.println("Does the string start with the specified character at the given index? " +a.startsWith("h", 1));
  System.out.println("Does the string start with the specified characters at the given index? " +a.startsWith("is", 5));
}
}

Code Explanation

  • Initialize the string and display it.
  • Now, use the “startsWith()” method to check if the string starts from the specified character at the given index.
  • Lastly, apply the same check for the set of characters i.e., “is” in the string as well at the specified index.

Output

In this output, it can be seen that since the character “h” and the set of characters “is” start from the specified index in the string, the boolean value “true” is returned. However, this is not the case with the character “q”.

Example 3: Applying the “startsWith()” Method With a String Instance

In this demonstration, the “startsWith()” method checks for the starting characters in a string instance instead:

package jbArticles;
public class Stringstartswith {
public static void main(String args[]) {
  String a = new String("This is Harry");
  System.out.println("Given String -> "+a);
  System.out.println("Does the string start with the specified character? " +a.startsWith("T"));
  System.out.println("Does the string start with the specified set of characters at the given index? " +a.startsWith("is", 5));
}
}

Code Explanation

  • Create a string instance using the “new” keyword followed by the “String()” constructor containing the given string. 
  • Now, apply the “startsWith()” method to check for the starting character in the string instance.
  • Lastly, use the method again to check for the starting set of characters at the given index in the string.

Output

Example 4: Applying the “startsWith()” Method With an Empty String Condition

If an empty string is added at the start of the string, then this empty string has no impact on the other string. This implies that it can be said that every string starts with an empty string. The following example code explains the discussed concept:

package jbArticles;
public class Stringstartswith {
public static void main(String args[]) {
  String a = "This is Harry";
  String b = "" + "This is David";
  System.out.println("Does the first string start with an empty string? " +a.startsWith(""));
  System.out.println("Does the second string start with an empty string? " +b.startsWith(""));   System.out.println("Does the second string start with the specified character? " +b.startsWith("T"));
}
}

Code Explanation

  • Initialize two strings such that a null string is added to the second string.
  • Now, use the “startsWith()” method with both strings to check if these strings start with an empty string.
  • Bonus Step: Finally, check if the first character in the second string is “T” or not. 

Output

All the returned boolean values “true” confirm the discussed statement that the string starts with an empty string and this empty string doesn’t affect the other string.

What is the Difference Between “charAt()” and “startsWith()” in Java?

Both these methods are used to analyze a particular character for its value. The “charAt()” method returns the string character at the specified index whereas the “startsWith()” method checks if a string starts with the given character.

The “charAt()” method has slightly better performance as compared to the “startsWith()” method. However, the “startsWith()” method has a bit more overhead.

Conclusion

The String “startsWith()” method in Java checks if the given string starts with the specified character/set of characters or not and returns the Boolean values “true” or “false” based on that. This method can also check if a string starts with a particular character/set of characters at the given index.