Java string method notes

Posted by The Stewart on Tue, 08 Feb 2022 01:45:36 +0100

Welcome to study

This paper mainly introduces the common methods of Java String class
Date:
2021/5/29

catalogue

  1. The most basic method of string
  2. Comparison between strings
  3. Conversion between string and other data types
  4. Search of characters and strings
  5. Interception and splitting of strings
  6. String replacement and modification

1, String basic method

1. Gets the length of the string

Method length ()

String str = new String("abc");
System.out.println(str.length());
//The output is 3

2. Gets the character of index i in the string

Method charAt(i)

String str = new String("abc");
int i = 1;
System.out.println(str.charAt(i));//Gets the character with index 1 in the string
//Output as b

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

Format: char array[] = new char[80]// First, create a char array with sufficient capacity, and the array name is arr.

usage method:
str.getChars(indexBegin,indexEnd,arr,arrayBegin);

The four parameters in parentheses point to the following meanings:

(1) indexBegin: the starting index of the string to be copied

(2) indexEnd: end index of the string to be copied, indexEnd-1

(3) arr: array name of char type array defined above

(4) arrayBegin: the index number at the beginning of array storage

        String str= new String("It is our bounden duty to revitalize China!");
        char[] arr =new char[10];
        str.getChars(0,4,arr,0);//Take the characters with index 0-4 and store them in arr to revitalize China
        System.out.println(arr);//Array arr indexes 0-4 are stored separately

2, String comparison

Introduction: 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; The second method is to compare whether two strings are equal. In this way, there are only two comparison results, true and false.

1. Comparison of the first string size

(1) CompareTo (other STR) is the size comparison method of strings without ignoring the case of strings
Format: int result = STR1 compareTo(str2);

     String str1="abc";
     String str2="abd";
     int result = str1.compareTo(str2);//-1

Output three comparison results:
If the Unicode value of the string is less than the Unicode value of the parameter string, the resu lt returns a negative integer;
If the Unicode value of the string = the Unicode value of the parameter string, the result returns 0;
If the Unicode value of the string > the Unicode value of the parameter string, the result returns a positive integer.

(2) Comparetoignorecase (other STR) is the size comparison method of string when the case of string is ignored
Format: int result = STR1 compareToIgnoreCase(str2);

        String str1="abc";
        String str2="abd";
        int result = str1.compareToIgnoreCase(str2);//-1

When ignoring the case of the string, three comparison results are returned: three comparison results are output: if the Unicode value of the string is less than the Unicode value of the parameter string, the result returns a negative integer; If the Unicode value of the string = the Unicode value of the parameter string, the result returns 0; If the Unicode value of the string > the Unicode value of the parameter string, the result returns a positive integer.

unicode encoding value table: Click me to jump

2. Comparison of the first string value
(1) Eaquals (other STR), a method to judge the equality of strings without ignoring the case of strings

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

        String str1="abc";
        String str2="ABC";
        boolean result = str1.equals(str2);//flase

(2) Equalsignorecase (other STR) is a method to judge the equality of strings when ignoring the case of strings
Format: boolean result = STR1 equals(str2);

        String str1="abc";
        String str2="ABC";
        boolean result = str1.equalsIgnoreCase(str2); //true

be careful:
What's the difference between equals and = =?
answer:

equals determines whether two variables or instances point to the same memory space

Determine whether the memory = = variable points to the same instance or not

For example, = = is to judge whether two people live in the same address, while equals is to judge whether the people living in the same address are the same

3, Conversion between string and 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 realize the conversion between integer and floating-point types. Therefore, the data type conversion method provided by String class is required for "20" and "20", which belong to different types of data.

boolean bool = Boolean.getBoolean("false"); //Convert string type to boolean type
int integer = Integer.parseInt("20"); //Convert string type to integer
long LongInt = Long.parseLong("1024"); //Convert string type to long integer
float f = Float.parseFloat("1.521"); //Convert string type to single precision floating point
double d = Double.parseDouble("1.52123");//Convert string type to double precision floating point
byte bt = Byte.parseByte("200"); //Convert string type to byte type
char ch = "Chess brother".charAt(0);
/***Convert other data types to string type method 1***/
String strb1 = String.valueOf(bool); //Converts a boolean type to a string type
String stri1 = String.valueOf(integer); //Converts an integer to a string type
String strl1 = String.valueOf(LongInt); //Converts a long integer to a string type
String strf1 = String.valueOf(f); //Converts a single precision floating-point type to a string type
String strd1 = String.valueOf(d); //Convert double type to string type
String strbt1 = String.valueOf(bt); //Convert byte to string type
String strch1 = String.valueOf(ch); //Convert character type to string type

To be added