Common methods for String classes in Java

Source: Internet
Author: User

Strings are widely used in Java programming, where strings belong to objects, and the string class provides many ways to handle strings, such as getting the length of a string, intercepting a string, converting a string to uppercase or lowercase, string splitting, and so on.

Common methods of the String class:

Precautions:

1. The index of characters in the string Str starts from 0 and ranges from 0 to Str.length ()-1

2. When using IndexOf for character or string lookups, if the match returns a position index, or 1 if there is no matching result

3. When using substring (beginindex, endIndex) for string interception, include characters from the beginindex position, excluding characters from the EndIndex position

What is the difference between "= =" and Equals ()

= =: Determines whether the first address in memory of two strings is the same, that is, whether it is the same string object

Equals (): Compares the content stored in two string objects in a consistent

Example: Determine if the Java file name and mailbox format are correct

 Public classTest { Public Static voidMain (string[] args) {//Java file nameString fileName = "Helloworld.java"; //EmailString email = "[Email protected]"; //determine if the Java file name is correct: The valid file name should end in. Java//gets the name of the last occurrence of "." Location of the number        intindex = Filename.lastindexof ('. ')); //get the suffix of a fileString prefix =filename.substring (index+1, Filename.length ()); //The judgment must contain the "." Number, and cannot appear in the first place, with the suffix named "java"        if(Index!=-1&&index!=0&&prefix.equals ("Java") {System.out.println ("Java file name is correct"); } Else{System.out.println ("Invalid Java file name"); }        //determine if the mailbox format is correct: at least "@" should be included in the legal mailbox name, and "@" is in "." Before//get the location of the "@" symbol in the mailbox        intIndex2 = Email.indexof (' @ ')); //get the "." In the mailbox Location of the number        intIndex3 = Email.lastindexof ('. ')); //The judgment must contain the "@" symbol, and "@" must be in the "." Before        if(Index2! =-1 && index3 >index2) {System.out.println ("Mailbox format is correct"); } Else{System.out.println ("Invalid mailbox Format"); }    }}

Common methods for String classes in Java

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.