How to Check If a String starts with Another String in Java


Here this example demonstrates how to determine whether a string starts with an input string value using the startsWith() method, as shown below.

Source Code

package com.beginner.examples;

public class StartWithString {

	public static void main(String[] args) {
		String str = "I love java";

		//Retrieves whether the string starts with "I"
		boolean b = str.startsWith("I");
		
		//Start with the second index
		boolean b2 = str.startsWith("love", 2);

		System.out.println("Is this string start with an "I": " + b);
		
		System.out.println("Whether to start with "love" from the second index in this string: "+b2);
		
		

	}

Output:

Is this string start with an "I": true
Whether to start with "love" from the second index in this string: true
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments