String startWith example in java

The startWith method of String class tests if this string starts with the specified prefix.

// String startWith example in java
public class StringStartWithExample {
    public static void main(String[] args) {
        // declare the String with some text
        String string = "You need to search in this string";
        // check whether String ends with this or not.
        // Use startsWith method of the String class to check the same.
        // startsWith() method returns true if a string is ending with a given word
        // otherwise it returns false
        boolean startsWith = string.startsWith("You");
        // Check given string strat with or not
        if (startsWith) {
            System.out.println("The given string starts with 'You'");
        } else {
            System.out.println("The given string does not starts with 'You'");

The output of above example will be as:

The given string starts with 'You'