This Java example shows how to check if string starts with another string using the startsWith
method, using a regular expression or using case insensitive comparison.
How to check if the string starts with another string in Java?
You can use the startsWith
method of the String class to check.
1 |
public boolean startsWith(String str) |
The startsWIth
method returns true if the string starts with the specified string, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.stringexamples; public class StringStartsWithExample { public static void main(String[] args) { String str = "Hello World"; boolean isStartsWith = str.startsWith("Hello"); System.out.println(str + " starts with Hello: " + isStartsWith); } } |
Output
1 |
Hello World starts with Hello: true |
Note:
The startsWith
method always returns true for an empty string or if the string is equal to the argument string. For example,
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javacodeexamples.stringexamples; public class StringStartsWithExample { public static void main(String[] args) { String str = "Hello World"; System.out.println(str.startsWith("")); System.out.println(str.startsWith(str)); } } |
Output
1 2 |
true true |
How to check if string starts with any of the given strings?
1) Using OR operator
Suppose you want to check if the string starts with any of the given multiple string values. Since the startsWith
method accepts only one argument, you need to check it multiple times using the OR (||) operator as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javacodeexamples.stringexamples; public class StringStartsWithExample { public static void main(String[] args) { String strDay = "Sunday"; if(strDay.startsWith("Sun") || strDay.startsWith("Sat")){ System.out.println("Weekend"); }else{ System.out.println("Weekday"); } } } |
Output
1 |
Weekend |
2) Using a regular expression
You can also use a regular expression to check as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.javacodeexamples.stringexamples; public class StringStartsWithExample { public static void main(String[] args) { String strDay = "Saturday"; if( strDay.matches("^(Sat|Sun).*$") ) System.out.println("Weekend"); else System.out.println("Weekday"); } } |
Output
1 |
Weekend |
We have used the “^(Sat|Sun).*$” pattern to check if the day starts with the string “Sat” or “Sun”, where
1 2 3 4 |
^ - start of the String (Sat|Sun) - contains "Sat" or "Sun" .* - followed by any characters $ - end of the String |
3) Using the Apache Commons library
If you are using the Apache Commons library, you can use the startsWithAny
method of the StringUtils class to check as given below.
1 2 3 4 5 6 |
String strDay = "Saturday"; if( StringUtils.startsWithAny(strDay, new String[]{"Sat", "Sun"}) ) System.out.println("Weekend"); else System.out.println("Weekday"); |
Output
1 |
Weekend |
How to check using case insensitive comparison?
By default, the startsWith
is case sensitive. For example,
1 |
System.out.println( "Sat".startsWith("sat") ); |
will return “false”.
You can use the toLowerCase
(or the toUpperCase
) method to change the case of both the strings as given below.
1 2 3 4 5 6 |
String strDay = "Saturday"; boolean isWeekend = strDay.toLowerCase().startsWith( "sat" ) || strDay.toLowerCase().startsWith( "sun" ); System.out.println(isWeekend); |
Output
1 |
true |
If you are using the Apache Commons library, you can use the startsWithIgnoreCase
method of the StringUtils class to do case insensitive comparison as given below.
1 2 |
String strDay = "Saturday"; System.out.println( StringUtils.startsWithIgnoreCase(strDay, "SAT") ); |
Output
1 |
true |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.