Export (0) Print
Expand All
Expand Minimize

java.lang.String.startsWith(String prefix)

Visual Studio 2005

Determines whether the current String starts with the contents of another String.


public boolean startsWith(String prefix)

Parameters

Parameter Description

prefix

The String whose contents will be compared against the beginning of the current String.

true if the current String starts with the contents of prefix; false otherwise.

// string_startswith1.jsl

public class Program
{
    public static void main(String[] args)
    {
        String str1 = "Fred is my name.";
        String str2 = "Xiao Mei is my name.";

        // The String to check the above two Strings to see
        // if they start with this value (Xi).
        String startStr = "Xi";

        // Do either of the first two Strings start with startStr?
        boolean starts1 = str1.startsWith(startStr);
        boolean starts2 = str2.startsWith(startStr);

        // Display the results of the startsWith calls.
        System.out.println("\"" + str1 + "\" starts with " +
            "\"" + startStr + "\"? " + starts1);
        System.out.println("\"" + str2 + "\" starts with " +
            "\"" + startStr + "\"? " + starts2);
    }
}

/*
Output:
"Fred is my name." starts with "Xi"? false
"Xiao Mei is my name." starts with "Xi"? true
*/

Community Additions

ADD
Show:
© 2014 Microsoft