Java – String startsWith() method

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:

References:

Happy Coding !!
Happy Learning !!

Java - String substring() method
Java - String split() method