In this article, we will check or test whether string starts with specified prefix or NOT using String’s startsWith() method
1. String’s startsWith() method:
- This String method test whether invoking string starts with specified prefix or NOT
Note:
- there are 2 variants or overloaded startsWith() methods
- 1st variant checks over complete range of string i.e.; starting from 0th index-position
- Whereas 2nd variant test from specified offset (or specified index-position)
1.1 Method Signature:
public boolean startsWith(String prefix);
public boolean startsWith(String prefix, int toffset);
1.2 Parameters:
- prefix –> substring that need to be searched from invoking-string
- toffset –> this is applicable only for 2nd variant to specify from where to start searching specified-prefix (basically starting index-position)
1.3 Returns:
startsWith() method |
Returns |
public boolean startsWith(String prefix); | Returns boolean true, if the invoking-string starts with specified-prefix
Otherwise, returns false; |
public boolean startsWith(String prefix, int toffset); | Returns boolean true, if the invoking-string starts with specified-prefix starting from specified-offset
Otherwise, returns false; |
2. Examples on startWith() method:
2.1 To test whether invoking String starts with specified prefix
Method Signature:
public boolean startsWith(String prefix);
StringStartsWithMethod.java
package in.bench.resources.string.methods;
public class StringStartsWithMethod {
public static void main(String[] args) {
// Example 1: tests whether str starts with "Java"
String testStr1 = "Java is a super cool language";
boolean bStr1 = testStr1.startsWith("Java");
System.out.println("Whether \"" + testStr1
+ "\" starts with 'Java' : " + bStr1);
// Example 2: tests whether str starts with "Bench"
String testStr2 = "BenchResources.Net is a Java weblog";
boolean bStr2 = testStr2.startsWith("Bench");
System.out.println("\nWhether \"" + testStr2
+ "\" starts with 'Bench' : " + bStr2);
// Example 3: tests whether str starts with "class"
String testStr3 = "String class has useful methods";
boolean bStr3 = testStr2.startsWith("class");
System.out.println("\nWhether \"" + testStr3
+ "\" starts with 'class' : " + bStr3);
// Example 4: tests whether str starts with "Oscar"
String testStr4 = "Oscar is a highest award in films";
boolean bStr4 = testStr4.startsWith("Oscar");
System.out.println("\nWhether \"" + testStr4
+ "\" starts with 'Oscar' : " + bStr4);
// Example 5: tests whether str starts with "Grammy"
String testStr5 = "Grammy award is considered equivalent";
boolean bStr5 = testStr5.startsWith("award");
System.out.println("\nWhether \"" + testStr5
+ "\" starts with 'award' : " + bStr5);
}
}
Output:
Whether "Java is a super cool language"
starts with 'Java' : true
Whether "BenchResources.Net is a Java weblog"
starts with 'Bench' : true
Whether "String class has useful methods"
starts with 'class' : false
Whether "Oscar is a highest award in films"
starts with 'Oscar' : true
Whether "Grammy award is considered equivalent"
starts with 'award' : false
2.2 To test whether invoking string starts with specified prefix starting from specified offset
Method Signature:
public boolean startsWith(String prefix, int toffset);
StringStartsWithMethod2.java
package in.bench.resources.string.methods;
public class StringStartsWithMethod2 {
public static void main(String[] args) {
// Example 1: tests whether str starts with "super"
// starting from 10th index-position
String testStr1 = "Java is a super cool language";
boolean bStr1 = testStr1.startsWith("super", 10);
System.out.println("Whether \"" + testStr1
+ "\" starts with 'super' starting "
+ "from 10th index-position : " + bStr1);
// Example 2: tests whether str starts with "Net"
// starting from 15th index-position
String testStr2 = "BenchResources.Net is a Java weblog";
boolean bStr2 = testStr2.startsWith("Net", 15);
System.out.println("\nWhether \"" + testStr2
+ "\" starts with 'Net' starting "
+ "from 15th index-position : " + bStr2);
// Example 3: tests whether str starts with "class"
// starting from 5th index-position
String testStr3 = "String class has useful methods";
boolean bStr3 = testStr2.startsWith("class", 5);
System.out.println("\nWhether \"" + testStr3
+ "\" starts with 'class' starting "
+ "from 5th index-position : " + bStr3);
// Example 4: tests whether str starts with "Oscar"
// starting from 19th index-position
String testStr4 = "Oscar is a highest award in films";
boolean bStr4 = testStr4.startsWith("award", 19);
System.out.println("\nWhether \"" + testStr4
+ "\" starts with 'award' starting "
+ "from 19th index-position : " + bStr4);
// Example 5: tests whether str starts with "Grammy"
// starting from 0th index-position
String testStr5 = "Grammy award is considered equivalent";
boolean bStr5 = testStr5.startsWith("award", 0);
System.out.println("\nWhether \"" + testStr5
+ "\" starts with 'award' starting "
+ "from 0th index-position : " + bStr5);
}
}
Output:
Whether "Java is a super cool language"
starts with 'super' starting from 10th index-position : true
Whether "BenchResources.Net is a Java weblog"
starts with 'Net' starting from 15th index-position : true
Whether "String class has useful methods"
starts with 'class' starting from 5th index-position : false
Whether "Oscar is a highest award in films"
starts with 'award' starting from 19th index-position : true
Whether "Grammy award is considered equivalent"
starts with 'award' starting from 0th index-position : false
Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!