A collection of Java string functions

Source: Internet
Author: User
Tags int size split trim stringbuffer

The string in Java is also a series of characters. But unlike many other computer languages that handle strings as character arrays, Java handles strings as String objects. Using a string as a built-in object handler allows Java to provide a rich set of functionality features to facilitate processing of strings.  Here are some of the more frequently used functions and their associated descriptions.


string correlation function


1) substring ()


it has two forms, the first of which is: String substring (int startIndex)


The second type is: String substring (int startindex,int endindex)


2) concat () Connect two strings


Example: String s= "Welcome to";


String t=s.concat ("Anhui");


3 replace () replaces


it has two forms, the first of which is to replace all occurrences of a character in the calling string, in the form of the following:


String replace (char Original,char replacement)


Example: String s= "Hello". Replace (' l ', ' w ');


the second form is to replace another sequence of characters with a sequence of characters, in the form of the following:


String replace (charsequence original,charsequence replacement)


4 Trim () Remove the start and end spaces


5) Convert valueof () to String


6) toLowerCase () to lowercase


7) toUpperCase () converted to uppercase


8) Length () to get the lengths of the string


Example: Char chars[]={' A ', ' B '. ' C '};


string S=new string (chars);


int len=s.length ();


9) charAt () intercept a character


Example: Char ch;


ch= "abc". CHARAT (1);


return value is ' B '


) GetChars () intercepting multiple characters


void getChars (int sourcestart,int sourceend,char target[],int targetstart)


Sourcestart Specifies the subscript of the substring start character


sourceend Specifies the subscript for the next character after the substring ends. Therefore, the substring contains characters from Sourcestart to SourceEnd-1.


target Specifies the array of received characters


the subscript value at which to begin copying substrings in Targetstart target


Example: String s= "This is a demo of the GetChars method.";


Char buf[]=new char[20];


S.getchars (10,14,buf,0);


) getBytes ()

One way
replaces GetChars () is to store characters in a byte array, which is GetBytes ()


Example:


String s = "hello! Hello!"  ”;


byte[] bytes = S.getbytes ();


) ToCharArray ()


Example:


String s = "hello! Hello!"  ”;


char[] ss = S.tochararray ();


equals () and equalsignorecase () compare two strings


) regionmatches () is used to compare a specific region of a string with another specific region, and it has an overloaded form that allows the case to be ignored in comparisons.


boolean regionmatches (int startindex,string str2,int str2startindex,int numchars)


Boolean regionmatches (Boolean ignorecase,int startindex,string


Str2,int str2startindex,int numchars)


) StartsWith () and EndsWith ()

The
StartsWith () method determines whether to start with a specific string

The
Endwith () method determines whether to end with a specific string


equals () and = =


equals () method compares characters in a string object

The
= = operator Compares whether two objects refer to the same instance.


Example: String s1= "Hello";


string S2=new string (S1);


s1.eauals (S2); True


S1==s2;//false


) CompareTo () and comparetoignorecase () compare strings


) indexOf () and LastIndexOf ()


indexOf () finds the place where the character or substring first appears.


LastIndexOf () to find characters or substrings is the last place to appear.


) trim to space function


example: String t1 = "abc de";


System.out.println (T1.trim ());//Remove the opening and trailing spaces "ABC de"





Split string Split


String y = "ABC,DE,FG,HI,JK";


string[] y1 = Y.split (",");//intercept string All "," character


for (int i = 0; i < y1.length; i++) {


System.out.print (Y1[i]);//Output Results Abcdefghijk


  }


) append Add or insert a function


StringBuffer zz1 = new StringBuffer (z1);//Append Insert character


zz1.append (' | '). Append ("Hijk"). Append ('/'). Append ("LMN"). Append ("OPQ");


System.out.println ();


System.out.print (ZZ1);//output: ABCDEFG|HIJK/LMNOPQ





StringBuffer Construction Function

The
StringBuffer defines three constructors:


StringBuffer ()


stringbuffer (int size)


StringBuffer (String str)


StringBuffer (charsequence chars)


The following are the StringBuffer related functions:


1) Length () and Capacity ()


a stringbuffer current length can be obtained by the length () method, while the entire allocated space is obtained by the capacity () method.


2) ensurecapacity () Set the size of the buffer


void ensurecapacity (int capacity)


3) SetLength () sets the length of the buffer


void SetLength (int len)


4) CharAt () and Setcharat ()


Char charAt (int where)


void setcharat (int where,char ch)


5) GetChars ()


void getChars (int sourcestart,int sourceend,char target[],int targetstart)


6) Append () The string representation of any type of data can be connected to the end of the called StringBuffer object.


example: int a=42;


StringBuffer sb=new StringBuffer (40);


String s=sb.append ("a="). Append (a). Append ("!"). ToString ();


6 Insert () inserts a string


stringbuffer Insert (int index,string str)


stringbuffer Insert (int index,char ch)


stringbuffer Insert (int index,object obj)


7 Index Specifies the subscript for the position of the string to be inserted into the StringBuffer object.


8) Reverse () reverses the characters in the StringBuffer object


StringBuffer reverse ()


9) Delete () and Deletecharat () remove characters


stringbuffer Delete (int startindex,int endindex)


stringbuffer deletecharat (int loc)


replace () replaces


stringbuffer replace (int startindex,int endindex,string str)


) substring () Intercept substring


String substring (int startIndex)


String substring (int startindex,int endindex)

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.