String common methods in Java

Source: Internet
Author: User

common methods of string in Java1, Length () String Example:CharChars[]={' A ', ' B '. ' C}; String s=NewString (chars); intlen=s.length ();2, CharAt () intercepts a character example:Charch; CH= "abc". CHARAT (1); Return ' B ' 3, GetChars () intercepts multiple charactersvoidGetChars (intSourcestart,intSourceend,CharTarget[],intTargetstart) Sourcestart Specifies the subscript for the start character of the substring, sourceend specifies the subscript of the next character after the substring ends. Therefore, the substring contains from Sourcestart to Sourceend-1 of the characters.  The array of the received characters is specified by Target, and the subscript value starting to copy the substring in target is targetstart. Example: String s= "This is a demo of the GetChars method."; Charbuf[]=New Char[20]; S.getchars (10,14,buf,0);4, GetBytes () replaces GetChars () by storing characters in a byte array, which is getBytes (). 5, ToCharArray ()6, Equals (), and equalsignorecase () compare two strings7, Regionmatches () is used to compare a particular area of a string with another specific area, and it has an overloaded form that allows the case to be ignored in comparisons. BooleanRegionmatches (intStartindex,string str2,intStr2startindex,intnumChars)BooleanRegionmatches (BooleanIgnoreCase,intStartindex,string str2,intStr2startindex,intnumChars)8, StartsWith (), and EndsWith () StartsWith () methods determine whether to start with a specific string, and the Endwith () method determines whether to end with a specific string9, Equals () and = =the Equals () method compares the characters in a string object,==operator to compare whether two objects refer to the same instance. Example: String s1= "Hello"; String S2=NewString (S1); S1.eauals (S2); //trueS1==S2;//false10, CompareTo (), and comparetoignorecase () compare strings11, IndexOf (), and LastIndexOf () indexOf () to find the first occurrence of a character or substring. LastIndexOf () finds the character or substring that is the last occurrence. 12, SUBSTRING () It has two forms, the first is: String substring (intStartIndex) The second type is: String substring (intStartIndex,intEndIndex)13, concat () connect two strings14, replace () replaces it in two forms, the first of which is replaced by a character in the invocation string where all occurrences of a character are in the form: string replace (CharOriginalCharReplacement) Example: String s= "Hello". Replace (' l ', ' W ')); The second form is to replace another sequence of characters with one character sequence, in the form of the following: String replace (charsequence original,charsequence replacement)15, Trim () Remove the start and end spaces16, ValueOf () convert to String17, toLowerCase () Convert to lowercase18, toUpperCase () to uppercase19, the StringBuffer constructor StringBuffer defines three constructors: StringBuffer () StringBuffer (intsize) StringBuffer (String str) stringbuffer (charsequence chars) (1), Length (), and Capacity () a StringBuffer current length can be obtained through the length () method, while the entire assignable space is obtained through the capacity () method. (2), ensurecapacity () sets the size of the buffervoidEnsurecapacity (intcapacity) (3), SetLength () sets the length of the buffervoidSetLength (intLen) (4), CharAt () and Setcharat ()CharCharAt (intwhere)voidSetcharat (intwhereCharch) (5), GetChars ()voidGetChars (intSourcestart,intSourceend,CharTarget[],intTargetstart) (6), append () to connect a string representation of any type of data to the end of the called StringBuffer object. Example:intA=42; StringBuffer SB=NewStringBuffer (40); String s=sb.append ("A="). Append (a). Append ("!"). toString (); (7), insert () inserts a string stringbuffer insert (intindex,string str) StringBuffer Insert (intIndexCharch) StringBuffer Insert (intindex,object obj) index Specifies the subscript of the position where the string is inserted into the StringBuffer object. (8), reverse () reverses the characters in the StringBuffer object StringBuffer reverse () (9), delete (), and Deletecharat () delete characters StringBuffer Delete (intStartIndex,intEndIndex) StringBuffer Deletecharat (intLoc) (10), replace () replaces StringBuffer replace (intStartIndex,intendindex,string str) (11), substring () intercepts the substring string substring (intStartIndex) String substring (intStartIndex,intEndIndex) Example://the method given by string can be called directly Public classtest{ Public Static voidMain (string[] args) {String s= "Welcome to Java world!"; String S1= "Sun Java"; System.out.println (S.startswith ("Welcome"));//string starts with welcomeSystem.out.println (S.endswith ("World"));//string ends with worldString SL = S.tolowercase ();//Convert all to lowercaseString SU = S.touppercase ();//convert all to uppercaseSystem.out.println (SL); System.out.println (SU); String b= S.substring (11);//starting from 11th placeSystem.out.println (b); String C= S.substring (8,11);//ending at 11th place starting from eighth placeSystem.out.println (c); String D= S1.trim ();//Remove the trailing spacesSystem.out.println (d); String S2= "I am a programmer, I am learning java"; String e= S2.replace ("Me", "You"); System.out.println (e);intF = 5; String S3=string.valueof (f); System.out.println (S3); String S4= "I am, this, king"; String[] G= S4.split (","); System.out.println (g[0]); When you convert a string to a base type, for example,int, Integer.praseint (string s) when converting a base type to a string, for example,StaticString ValueOf (inti) Category: Java

String common methods 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.