JAVA string method, JAVA string method

Source: Internet
Author: User

JAVA string method, JAVA string method

 

1. length () String length

For example, char chars [] = {'A', 'B'. 'C '};

String s = new String (chars );

Int len = s. length ();

2. charAt () Intercepts one character

Example: char ch;

Ch = "abc". charAt (1); 'B' is returned'

3. getChars () Intercepts multiple characters

Void getChars (int sourceStart, int sourceEnd, char target [], int targetStart)

SourceStart specifies the subscript of the starting character of the substring, and sourceEnd specifies the subscript of the next character after the completion of the substring. Therefore, the substring contains characters from sourceStart to sourceEnd-1. The array of received characters is specified by the target. The value of the Child string that starts copying in the 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 byte arrays, that is, getBytes ().

5. toCharArray ()

6. Compare two strings: equals () and equalsIgnoreCase ().

7. regionMatches () is used to compare a specific region and another specific region in a string. It has an overloaded form that allows case-insensitive 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 reference the same instance.

Example: String s1 = "Hello ";

String s2 = new String (s1 );

S1.eauals (s2); // true

S1 = s2; // false

10. Comparison string between compareTo () and compareToIgnoreCase ()

11. indexOf () and lastIndexOf ()

IndexOf () is used to locate the first occurrence of a character or substring.

LastIndexOf () is the last occurrence of a character or substring.

12. substring ()

It has two forms: String substring (int startIndex)

The second type is: String substring (int startIndex, int endIndex)

13. concat () connects two strings

14. replace () Replacement

It can be in two forms. The first form is to replace all the places where a character appears in the call string with one character. The form is as follows:

String replace (char original, char replacement)

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

The second form is to replace another character sequence with one character sequence. The form is as follows:

String replace (CharSequence original, CharSequence replacement)

15. trim () removes spaces at the beginning and end.

16. Convert valueOf () to a string

17. toLowerCase () to lowercase

18. Convert toUpperCase () to uppercase

19. StringBuffer Constructor

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 by the length () method, and the entire allocable space is obtained by the capacity () method.

  

(2) ensureCapacity () sets the buffer size

Void ensureCapacity (int capacity)

(3) setLength () set the buffer Length

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 () indicates that the string of any data type is connected to the end of the called StringBuffer object.

For example, int a = 42;

StringBuffer sb = new StringBuffer (40 );

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

(7) insert () insert a string

StringBuffer insert (int index, String str)

StringBuffer insert (int index, char ch)

StringBuffer insert (int index, Object obj)

Index specifies the subscript of the position where the string is inserted into the StringBuffer object.

(8), reverse () reverse the characters in the StringBuffer object

StringBuffer reverse ()

(9), delete (), and deleteCharAt () delete characters

StringBuffer delete (int startIndex, int endIndex)

StringBuffer deleteCharAt (int loc)

(10) replace ()

StringBuffer replace (int startIndex, int endIndex, String str)

(11) and substring () are used to intercept substrings.

String substring (int startIndex)

String substring (int startIndex, int endIndex)

Copyright statement: I feel very grateful if I still write well, I hope you can use your mouse and keyboard to give me a thumbs up or give me a comment! _______________________________________________________________ You are welcome to reprint. I hope to add the original address while you reprint it. Thank you for your cooperation.

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.