Summary of common methods of String type (super detailed summary and super many codes)

Table of contents

1 Introduction

2. Common methods of String

2.1 Construction method

2.2 Comparison method of String object

2.3 String lookup

2.4 Conversion

2.4.1 Number and string conversion

2.4.2 Case conversion

2.4.3 String to Array

 2.5 String Replacement

 2.6 String splitting

 2.7 String interception


1 Introduction

Today, I will introduce the String reference type and its common methods.

2. Common methods of String

2.1 Construction method

 str1 // use constant string construction

 str2 // direct newString object

 str3 // use character array to construct

2.2 Comparison method of String object

1. == compares whether the same object is referenced . == compares in String: whether the same object is referenced. Obviously, the following code creates two different objects, which are different =
    want to compare whether the values ​​inside are equivalent , need to use equals to compare.

 2. boolean equals(Object anObject) method:     Use equlas to compare the values ​​in two strings according to the lexicographical comparison . There is no need to rewrite the equals method, because the String class has been rewritten at the bottom layer, just call it directly

3. compare To compares two strings, who is bigger and who is smaller. No need to rewrite, the String string class, the bottom layer rewrites this method by itself,
    if the length is equal, compare the difference between the first character that appears unequal (ASCII difference)
    If the length is not equal, and the first k characters If they are equal, return the difference between the lengths of the two characters

4. compareToIgnoreCase is to ignore case for comparison, return 0 to prove that it is the same string (ignore case)

5. equalsIgnoreCase ignores whether the values ​​in the size comparison strings are the same 

2.3 String lookup

Here are just some common methods

1. charAt(int index):
    Get the characters of the string (because the String type stores an array of characters, so you can access the characters in the string through the array subscript), be careful not to access the array out of bounds, otherwise An exception will be reported
    and printed out h

 2. indexOf method

(1) Return the position of the character that appears for the first time, without returning -1, the return value is int type

 (2) You can also specify which subscript to start searching from, here is to start searching from the position of subscript 1

 (3) You can also search for a string and return the position where the first character is found


3. lastIndexOf(int ch) 
(1) Search from the back to the front, and return the value of the subscript after finding it (the value is still counting from the front 0), the search starts from the back, and the number of subscripts starts from the front Subscript, so return 9 here

(2) Specify the subscript from which to search forward, here is to search forward from c, there is no g, so return -1;

 

(3) You can also search for a string from the back to the front

2.4 Conversion

2.4.1 Number and string conversion

(1) String.valueOf converts other types to character types.

(2) If an object is converted to a character type, the conversion result is as follows:

(3) To convert a string to an integer, use Integer.parseInt, the wrapper class of the int type, and you can also specify the conversion to 8/n base.

 

2.4.2 Case conversion

(1) toUpperCase: Change letters to uppercase, regardless of Chinese
(2) toLowerCase: Change to lowercase

Note: Changing the case does not change the original string, the above s1 remains unchanged, but generates a new object. 

2.4.3 String to Array

toCharArray (often used), will not use the original string, or create a new object to convert it into an array, because the string itself cannot be modified, and it is private modified

        array to string

 2.5 String Replacement

1. s1.replace has many overloaded methods, here you can change all a in the original string into T

 

2. It can realize the replacement of strings, and can also replace the first string that appears, and the rest will not be replaced

 2.6 String splitting

 1. Use spaces as separators for splitting. The return value of s1.split is an array, and each element can be placed in each subscript. Here, it is divided into 3 groups, which are placed under 0, 1, and 2 of the array respectively. marked at.

2. Special segmentation

(1) The delimiter "." is quite special and must be separated by \\.

(2) \ must be separated by \\, where \\ is actually the meaning of \

 

 (3) There can be multiple separators, just use | in the middle, here are spaces and & are both separators, divided into two groups, respectively placed in two array subscripts

 3. Multiple splits

First use the " " delimiter to split into two elements in an array,

Then use & to split

 2.7 String interception

1. The str.substring method can specify where to intercept, for example, intercept from the position where the subscript is 2, and print it out as cdef;

2. You can specify the interception range, such as intercepting from the subscript 1 to the subscript 3, note that it is left closed and right open. The place where the subscript is 3 cannot be intercepted, so print out bc.

Guess you like

Origin blog.csdn.net/weixin_44580000/article/details/125498961