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