Java class notes------string API

Source: Internet
Author: User

*intLength ()*gets the number of characters (length) in the current string, both Chinese and English*Each character is counted as a length str.length (); *intindexOf (String str)*finds the position of the given string for the first occurrence in the current string. *The return value is the first character of the given string in the current string subscript*because Java is case sensitive, you must match all of the above to return a specific subscript. Index= Str.indexof ("in"); *intIndexOf (String str,intFrom )*Overloaded methods:*finds the position of the first occurrence of the given string, starting at the specified subscript position in the current string. * Similarly, if not found will return-1. Index= Str.indexof ("In", 3); *intlastIndexOf (String str)*finds the position of the last occurrence of the given string in the current string index= Str.lastindexof ("in"); *get some of the contents of a string* String substring (intStartintend)*starts from the specified subscript start and intercepts the specified subscript end*Note that the character that does not contain the end position is the string between the points. *the Java API typically uses 2 numbers to denote a range, usually* "With head does not contain tail"the. Str.substring (4,9); *String trim ()*removes blank string trim from both sides of the current string=Str.trim (); *CharCharAt (intindex)*gets the character that corresponds to the specified position in the current stringCharc = Str.charat (9); *BooleanStartsWith (String str)*BooleanEndsWith (String str)*determines whether the current string begins with the given string, and the other*is whether the end of the given string is judged. BooleanStarts = Str.startswith ("Think"); BooleanEnds = Str.endswith ("Ava"); *String touppercase ()*String toLowerCase ()*converts the English part of the current string to all-uppercase and all-lowercase string upper=str.touppercase (); String Lower=str.tolowercase (); //usually used to ignore case judgments (e.g. verification code judgment)*StaticString valueOf ()*The string provides a number of static methods valueof ()*the effect is to convert other types to strings. The more commonly used is to*the base type is converted to a string. String S1= String.valueof (123);//"123"String S2= String.valueof (123.123);//"123.123"//The result of any type-to-string connection is a string. String s3 = 123+ ""; *Java.lang.StringBuilder*internally maintains a variable array of characters. To resolve frequent modifications*the performance loss caused by string content. *A related method for editing a string is provided internally:*additions and deletions StringBuilder SB=NewStringBuilder ("123456"); *StringBuilder Append (String str)*appends the given content to the end of the current string Sb.append ("789"); //Gets the StringBuilder internal edited stringstr =sb.tostring (); * StringBuilder Delete (intStartintend)*deletes a string from the current string within a given range Sb.delete (1, 2); * StringBuilder Insert (intoffset,string str)*inserts the given string content at the specified position Sb.insert (2, "456"); * StringBuilder Replace (intStartintend,string str)*replaces a string in the given range in the current string with the given string Sb.replace (0, 2, "456"); //Invert stringSb.reverse (); *Booleanmatches (String regex)*verifies that the current string satisfies the formatting requirements of a given regular expression* Note: Whether or not the regular expression uses "^", "$"are all*Match Validation String str= "[Email protected]"; * [A-za-z0-9_][email protected][a-za-z0-9_]+ (\.[ a-z]+) +String regex= "[A-za-z0-9_][email protected][a-za-z0-9_]+ (\\.[ a-z]+) + "; BooleanMatches =str.matches (regex); *string[] Split (String regex)* "Cut off" the part of the current string that satisfies the regular expression, keep the remainder.        Splits a string operation. String Str= "ABC123JH452FH748HF870SDFS5AQ75QEW"; String[] Array= Str.split ("[0-9]+"); *string ReplaceAll (String regex,string str)*replaces the content in the current string that satisfies the regular expression part with the*the string given by string str= "Fhhf132fhg67fh606fhf234assd798dgh"; /** Replace the number part in the current string with "#NUMBER #"*/Str= Str.replaceall ("\\d+", "#NUMBER #"); 

Java class notes------string API

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.