"Go" Java intercept string

Source: Internet
Author: User

Original URL: http://blog.csdn.net/yelangjueqi/article/details/9149699

Common methods for intercepting strings:

1 length () Lengths of string
Example: Char chars[]={' A ', ' B '. ' C '};
String S=new string (chars);
int Len=s.length ();

2. CharAt () intercepts a character
Example: Char ch;
Ch= "abc". CHARAT (1); Return ' B '

3. GetChars () intercepts multiple characters
void GetChars (int sourcestart,int sourceend,char target[],int targetstart)
SOURCESTART Specifies the subscript for the start character of the substring, and sourceend specifies the subscript for the next character after the substring ends. Therefore, the substring contains characters from Sourcestart to SourceEnd-1. 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.";
Char Buf[]=new char[20];
S.getchars (10,14,buf,0);

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

5, ToCharArray ()

6, Equals () and equalsignorecase () compare two strings

7, 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.
Boolean regionmatches (int startindex,string str2,int str2startindex,int numChars)
Boolean regionmatches (Boolean ignorecase,int startindex,string str2,int str2startindex,int numChars)

8, StartsWith () and EndsWith ()
The StartsWith () method determines whether to start with a specific string, and the Endwith () method determines whether to end with a specific string

9, Equals () and = =
The Equals () method compares the characters in a string object, and 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

10, CompareTo () and comparetoignorecase () compare strings

11, IndexOf () and LastIndexOf ()
IndexOf () finds the first occurrence of a character or substring.

String a= "ADAFDSAFS,HEDFD";
String b=a.substring (0, A.indexof (","));
System.out.println (b); Adafdsafs


LastIndexOf () finds the character or substring that is the last occurrence.

12, SUBSTRING ()
It has two forms, the first of which is: String substring (int startIndex)
The second type is: String substring (int startindex,int endIndex)

13, concat () connect two strings

14, replace () replacement
It has 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 (char Original,char replacement)
Example: String s= "Hello". Replace (' l ', ' w ');
The second form is to replace another sequence of characters with one character sequence, in the following form:
String replace (charsequence original,charsequence replacement)

15. Trim () Remove the start and end spaces

16, ValueOf () converted to string

17, toLowerCase () converted to lowercase

18. Convert toUpperCase () to uppercase

19. StringBuffer constructor function
StringBuffer defines three constructors:
StringBuffer ()
StringBuffer (int size)
StringBuffer (String str)
StringBuffer (charsequence chars)
  
(1), Length () and Capacity ()
The current length of a stringbuffer 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 buffer
void ensurecapacity (int capacity)

(3), SetLength () set 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 concatenated 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 ();

(7), insert () inserts a string
StringBuffer Insert (int index,string str)
StringBuffer insert (int index,char ch)
StringBuffer Insert (int index,object obj)
index specifies the subscript for inserting a string into the position of 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)

(10), replace () replacement
StringBuffer replace (int startindex,int endindex,string str)

(11), substring () intercept sub-string
String substring (int startIndex)
String substring (int startindex,int endIndex)

"Go" Java intercept string

Related Article

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.