Java Fundamentals Hardening the judgment function of the string class of 33:string class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.