Check if a Java String Starts with a Specific String

Use the String.startsWith() method when you need to check whether a Java string begins with a specific prefix. It returns a boolean value: true when the prefix matches the beginning of the string, and false otherwise.

This tutorial covers the standard startsWith() method, checking from a specific offset, matching any of several prefixes, handling case-insensitive checks, and important null and empty-string behavior.

The following existing shorthand shows the basic startsWith() call.

</>
Copy
 String.startsWith(String str)

The startsWith() method checks whether the beginning of the current string matches the prefix passed as its argument. In Java code, the method is called on a String instance, as shown in the examples below.

</>
Copy
boolean result = string.startsWith(prefix);

These quick checks show that only text at the beginning of the string is considered.

</>
Copy
"tutorialkart".startsWith("tutorial"); //true
"tutorialkart".startsWith("kart"); //false
"tutorialkart".startsWith("tut"); //true
"tutorialkart".startsWith("al"); //false

The following programs demonstrate both the built-in method and a manual prefix comparison.

1. Example – Check If string starts with a specific value in Java

In this example, we have taken two strings: str1 and str2. After that we shall check if str1 starts with str2 using String.startsWith() method.

CheckIfStringStartsWith.java

</>
Copy
/**
 * Java Example Program to Check if String starts with
 */

public class CheckIfStringStartsWith {

	public static void main(String[] args) {
		String str1 = "www.tutorialkart.com";
		String str2 = "www";

		boolean b = str1.startsWith(str2);
		System.out.print(b);
	}
}

Run the above program and you should get the following output in console.

Output

true

2. Example – Check if given website starts with “www” in Java

In this example, we have written a custom function, to check if a string starts with the specified string.

CheckIfStringStartsWith.java

</>
Copy
/**
 * Java Example Program to Check if String starts with
 */

public class CheckIfStringStartsWith {

	public static void main(String[] args) {
		System.out.println(ifStartsWith("www.tutorialkart.com", "www"));
	}

	/**
	 * Checks if str1 starts with str2
	 * @param str1
	 * @param str2
	 * @return return true if str1 starts with str2, else return false
	 */
	public static boolean ifStartsWith(String str1, String str2) {
		if(str1.length()>=str2.length()) {
			if(str1.substring(0, str2.length()).equals(str2)) {
				return true;
			}
		}
		return false;
	}
}

Run the program.

Output

true

How Java String startsWith() Checks a Prefix

The startsWith() method returns true when the beginning of a string matches the specified prefix. Otherwise, it returns false. The comparison is case-sensitive, so "Java" starts with "Ja", but it does not start with "ja".

The method is most suitable when you need to test a literal prefix such as a URL scheme, filename prefix, command keyword, product code, or other fixed text. A regular expression is not required for a simple prefix check.

Java startsWith() Return Values for Common Prefix Checks

</>
Copy
String text = "Java Programming";

System.out.println(text.startsWith("Java"));
System.out.println(text.startsWith("Programming"));
System.out.println(text.startsWith("java"));
System.out.println(text.startsWith(""));

The first call returns true because the string begins with "Java". The second and third calls return false. The final call returns true because every Java string starts with the empty string.

true
false
false
true

Check a Java String Prefix from a Specific Offset

Java also provides an overloaded startsWith() method that checks whether a prefix begins at a specified offset within the string. This is useful when the text before the prefix is known and you do not want to create a substring first.

</>
Copy
boolean startsWith(String prefix, int toffset)

Here, prefix is the text to match and toffset is the zero-based position where the comparison should begin.

</>
Copy
String text = "Learn Java";

System.out.println(text.startsWith("Java", 6));
System.out.println(text.startsWith("Java", 0));
true
false

At offset 6, the remaining text begins with "Java". At offset 0, the string begins with "Learn", so the second check returns false.

Check if a Java String Starts with Any of Multiple Prefixes

If more than one prefix is acceptable, combine multiple startsWith() checks with the logical OR operator. This keeps the intent clear when the list is short.

</>
Copy
String url = "https://www.tutorialkart.com";

boolean isWebUrl = url.startsWith("http://") || url.startsWith("https://");

System.out.println(isWebUrl);
true

For a larger collection of allowed prefixes, you can store the prefixes in a collection and test them with a loop or stream instead of writing a long chain of OR conditions.

Check a Java String Prefix Without Case Sensitivity

startsWith() is case-sensitive and does not have an ignore-case overload. If your requirement is case-insensitive prefix matching, regionMatches() can compare the beginning of the string while ignoring case.

</>
Copy
String text = "TutorialKart";
String prefix = "tutorial";

boolean result = text.regionMatches(true, 0, prefix, 0, prefix.length());

System.out.println(result);
true

The first argument, true, tells regionMatches() to ignore case. The comparison begins at index 0 in both strings and checks the number of characters in the prefix.

startsWith() Compared with substring(), Regex, and endsWith()

  • Use startsWith() when the requirement is a literal prefix check.
  • Use startsWith(prefix, offset) when the prefix should be checked from a known position inside the string.
  • Use a regular expression only when the beginning of the string must match a pattern rather than fixed text.
  • Use endsWith() when you need to test the end of the string instead of the beginning.
  • A manual substring(...).equals(...) check can work, as shown earlier, but startsWith() expresses the requirement more directly and avoids creating a separate substring for the comparison.

Null, Empty, and Case-Sensitive Prefix Behavior in Java

Keep these edge cases in mind when checking prefixes:

  • If the prefix is an empty string, startsWith("") returns true.
  • If the prefix argument is null, calling startsWith(null) throws a NullPointerException.
  • If the string reference itself is null, calling any instance method such as startsWith() on it also throws a NullPointerException.
  • Character case must match unless you deliberately use a case-insensitive approach such as regionMatches().
</>
Copy
String text = null;
String prefix = "Java";

boolean result = text != null && prefix != null && text.startsWith(prefix);

System.out.println(result);
false

Performance of Java String startsWith()

A prefix check does not need to compare the entire string when the prefix is shorter. In the worst case, startsWith() compares characters across the prefix until it either finds a mismatch or reaches the end of that prefix. If the prefix length is m, the comparison is therefore proportional to m.

For normal application code, this direct prefix comparison is preferable to creating a substring solely to compare it with another string.

Java startsWith() Editorial QA Checklist

  • Verify that every literal prefix example uses startsWith() rather than an unnecessary regular expression.
  • Confirm that case-sensitive examples do not imply that "Java" and "java" match.
  • Check that examples using the offset overload treat the offset as a zero-based index.
  • Keep null-handling examples explicit so readers do not assume startsWith(null) is safe.
  • Use endsWith() only for suffix checks and do not describe it as equivalent to startsWith().
  • When several prefixes are accepted, ensure the code tests each intended prefix and does not accidentally test whether the prefix occurs elsewhere in the string.

Java String startsWith() Summary

In this Java Tutorial, we learned how to check if a String starts with another string.

For a fixed prefix, String.startsWith() is the clearest choice. Java also provides an offset overload for checking from a specific position, while regionMatches() can be used when the prefix comparison must ignore case.