Detailed sorting of Java string class operation methods

The basic operations of string class can be divided into the following categories:

1. Basic operation method

2. String comparison

3. Conversion between string and other data types

4. Search for characters and strings

5. String interception and splitting

6. String replacement and modification

I think I need to remind myself before sorting out the following contents. We don't need to manually guide the package like other operations. Its string, StringBuffer and other classes are encapsulated in Java Lang package, we can directly call the string method!

First, let's talk about the basic operation methods. The basic operation methods of string include the following:

(1) get string length ()

(2) get charat (I), the ith character in the string

(3) get the character method getchars at the specified position (4 parameters)

1. Method of obtaining string length

Format: int length = str.length();

2. Method of obtaining the ith character in the string

Format: char ch = str.charat (I)// I is the index number of the string. The characters at any position of the string can be obtained and saved to the character variable

3. How to obtain the character at the specified position

Format: char array [] = new char [80]// First, create a char array with a large enough capacity. The array name is array

    str.getChars(indexBegin,indexEnd,array,arrayBegin);

Explain the meaning of the four parameters in parentheses:

1. Indexbegin: the starting index of the string to be copied

2. Indexend: the end index of the string to be copied, indexend-1

3. Array: the array name of the char type array defined above

4. Arraybegin: the index number at the beginning of array storage

In this way, we can copy all the characters in the desired range in the string to the character array and print out the character array.

Methods similar to getchars () have a GetBytes (), which are basically the same, except that the GetBytes () method creates an array of byte type, and the byte encoding is the default character set encoding, which is a character represented by encoding.

Here is a brief demonstration of the usage of the three methods:

The operation results are as follows:

2、 String comparison

We know that explicit values can be easily compared, so how should strings be compared? String comparison is to compare the two strings character by character from left to right. The comparison is based on the uncoded value of the current character until the size of two different characters is compared.

String comparison can also be divided into two categories: one is the comparison of string size. Such comparison has three results: greater than, equal to and less than; Another kind of comparison method is to compare whether two strings are equal. In this way, there are only two comparison results, true and false.

1. First, let's take a look at the first method of comparing size requirements:

(1) Size comparison method of string under

Format: int result = STR1 compareTo(str2);

Three comparison results are output: if the Unicode value of the string is the Unicode value of the parameter string, the result returns a positive integer.

(2) String size comparison method

Format: int result = STR1 compareToIgnoreCase(str2);

In case of ignoring string case, three comparison results are returned: three comparison results are output: if the Unicode value of the string is the Unicode value of the parameter string, the result returns a positive integer.

2. Then take a look at the second method to judge whether the two strings are equal (in case of equality, the length of the two strings must be equal):

(1) A method to distinguish the equality of strings under

Format: boolean result = STR1 equals(str2);

If and only if the lengths of STR1 and STR2 are equal and the Unicode encoding of the corresponding position characters are exactly the same, return true; otherwise, return false

(2) A method to distinguish the equality of strings under

Format: boolean result = STR1 equals(str2);

The demo is as follows:

The operation results are as follows:

3、 Conversion of strings to other data types

Sometimes we need to make a conversion between the string and other data types, such as changing the string data into integer data, or conversely changing the integer data into string data. "20" is the string, and 20 is the integer number. We all know that forced type conversion and automatic type conversion can be used to convert between integer and floating-point types, so the data type conversion method provided by string class is required for "20" and "20" which belong to different types of data.

Because there are many data types, there are many conversion methods. Here I will list them in a table:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>