1. The judging function of the string class:
Boolean equals (Object obj)boolean equalsignorecase (String str)boolean contains (string str) startsWith (String str)boolean EndsWith (String str)boolean isEmpty ()
2. Case:
1 Packagecn.itcast_03;2 3 /*4 * The judging function of the string class:5 * Boolean equals (Object obj): Compares the contents of strings for the same, case-sensitive 6 * Boolean equalsignorecase (String str): Compares the contents of a string for the same, ignoring case 7 * Boolean contains (String str): Determines whether a large string contains a small string 8 * Boolean startsWith (String str): determines whether a string begins with a specified string 9 * Boolean endsWith (String str): determines whether the string ends with a specified string Ten * Boolean isEmpty (): Determines whether the string is empty. One * A * Note: - * The string contents are empty and the string object is empty. - * String s = ""; String content is empty the * String s = null;//string object is empty - */ - Public classStringdemo { - Public Static voidMain (string[] args) { + //creating a String Object -String S1 = "HelloWorld"; +String s2 = "HelloWorld"; AString s3 = "HelloWorld"; at - //boolean equals (Object obj): Compares the contents of strings for the same, case-sensitive -System.out.println ("Equals:" +s1.equals (S2)); -System.out.println ("Equals:" +S1.equals (S3)); -System.out.println ("-----------------------"); - in //boolean equalsignorecase (String str): Compares whether the contents of a string are the same, ignoring case -System.out.println ("Equals:" +s1.equalsignorecase (S2)); toSystem.out.println ("Equals:" +S1.equalsignorecase (S3)); +System.out.println ("-----------------------"); - the //Boolean contains (String str): Determines whether a large string contains a small string *System.out.println ("contains:" + s1.contains ("Hello")); $System.out.println ("contains:" + s1.contains ("HW")));Panax NotoginsengSystem.out.println ("-----------------------"); - the //boolean StartsWith (String str): Determines whether a string begins with a specified string +System.out.println ("StartsWith:" + s1.startswith ("H")); ASystem.out.println ("StartsWith:" + s1.startswith ("Hello")); theSystem.out.println ("StartsWith:" + S1.startswith ("World"))); +System.out.println ("-----------------------"); - $ //Exercise: Boolean endsWith (String str): Determines whether a string ends with a specified string. $ - //boolean isEmpty (): Determines whether the string is empty. -System.out.println ("IsEmpty:" +s1.isempty ()); the -String S4 = "";WuyiString S5 =NULL; theSystem.out.println ("IsEmpty:" +s4.isempty ()); - //NullPointerException Wu //S5 object does not exist, so method cannot be called, NULL pointer exception -System.out.println ("IsEmpty:" +s5.isempty ()); About } $}
The results of the operation are as follows:
Java Fundamentals Hardening the judgment function of the string class of 33:string class