Strings and character arrays

Strings and character arrays

String operations and character array operations are the two most commonly used and dealt with in our interaction and implementation functions. However, it is always confused before, and in order to distinguish the conversion application skillfully, the following classification is unique.

First, the characters must be enclosed in single quotes''.

In Java, there is no basic data type that represents a string, but the String class is used to represent a string, which must be enclosed in double quotes "".

First of all, arrays can store basic data types or reference types. And only one data type can be stored .

Note: This means that in addition to basic data types, the rest are only references. (It can be a reference to the object, etc., but not the object itself).

It is precisely because reference types can be stored that the illusion that multiple data types can be stored (this is because the base class reference (pointer) points to the derived class object, this idea can be said to open up the inheritance of the entire class, but also supports The realization of the simple factory design pattern) Note [1]

There is no multidimensional array. The array itself is an array, that is to say, the array can store the array, but the multidimensional array is just an illusion

int[] represents this type of reference. To create an array is to create int[]

There is a length attribute in the array, which is used directly.

Use foreach(:) loop to traverse, no need to specify length.

String

First of all. Regarding String initialization, you can create it with a char array

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        char[] a={
    
    'a','b','c','d'};

        String s = new String(a);

        String s1 = new String (a,1,2);//a,offset,count

        String s2 = new String("dfskj"); 
    }
}

The connection string can use + directly;

All subsequent operations are to call the method of the string object

Encapsulating String into an object realizes empowerment, making String possess powerful functions and use tension.

.length(); //In the array is directly length, where the length is equivalent to an attribute of the array itself, which is determined with the creation of the array, so it also determines that the length of the array cannot be changed, and here length( ) This is equivalent to calling a method of the object, so the length can be changed without any restrictions, just call this method to calculate the length of the string

  • Search: (The return value is int).indexOf(); //The position of the first occurrence; lastIndexOf(); //The position of the last occurrence

  • Get: (return value char).charAt(int index)

  • **Get the sub-substring: **substring (start, end (do not write the default end)) return value String

  • Remove spaces: .trim();

  • String replacement : replace(char old, char new)

  • The judgment starts at the end : startsWith(); endsWith();

  • String separation : The result is stored in a string array: .split(String sign,int limit(number of splits)); //Split character, it can also be a regular expression

Conversion of String and Char Array

This part is easy to be ignored, but it is used a lot in practice.

Char[] array and String type interchange

1.String —> char[ ]

  • Method 1: toCharArray() method in String class

Such as:

String a = "liushiwennb";
char[ ] arr = a.toCharArray();
  • Method 2: through the charAt() method in the String class and an empty char array

This method uses String's length() method to determine the size of the char array first. Then use charAt() to return characters and assign values ​​with the help of loops.

Such as:

public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
        String a = "liushiwennb";
        char[ ] arr = new char[a.length()];  // 创建和a的长度一样的字符数组

        for (int b = 0; b < a.length(); b++){
    
    

            arr[b] = a.charAt(b);
        }
    }
}

2.char[ ] —> String

  • Method 1: Use the constructor of the String class, such as:
char[ ] arr; // 非空的字符数组

这里有点将 char数组作为一个target,来新建一个String对象。

String result = new String(arr);

 
  • 方法2:char[ ] --> StringBuffer --> String

    This is done with the help of the StringBuffer intermediate object as:

    char[] arr; // non-empty character array

    StingBuffer sb = new StringBuffer();

    sb.append(arr);

    String result = sb.toString(); 
  • Method 3: The valueOf() method in the String class, such as:
char[ ] arr; // 非空的字符数组

String result = String.valueOf(arr);
  • Method 4: Use an empty string, such as:
char[ ] arr; // 非空的字符数组

String result = "";  //创建一个空的字符串 这里是空的字符串,不是null(不存在)的字符串
for (int a = 0; a < arr.length; a++){
    
    
    result += arr[a];
}

5) Method 5: Use the toString() method in the Arrays class, such as:

char[ ] arr; // 非空的字符数组

String result = Arrays.toString(arr);
  • · Integer valueOf(int i) : Returns an Integer instance representing the specified int value.
  • · Integer valueOf(String s) : Returns an Integer object that holds the value of the specified String.
  • · Integer valueOf(String s, int radix) : Returns an Integer object that stores the value extracted from the specified String when the radix provided by the second parameter is used for parsing.
  • · Radix**** specifies the base

Because the toString method is an existing method in Object, and all classes inherit Object, so "all objects have this method".

I think that String is the one we use most in interactions, and I see its importance from the side, so it empowers it.

It is usually just for the convenience of output, such as System.out.println(xx), if the "xx" in the brackets is not a String type, the toString() method of xx is automatically called

All in all, it is just a method specially added in order to facilitate all kinds of string operations when Sun company develops java.

Note 1: Supplementary thoughts irrelevant to this article: the forced conversion of a derived class to a base class gives a pointer to the derived class to an object that can be the base class. But this is absolutely not allowed. The pointer of the derived class cannot point to the object of the base class. Because the memory space of the derived class is longer than that of the base class, access will cause memory overflow, so the pointer of the derived class is not allowed to point to the base class. The forced conversion to a base class is just an illusion. Coercion is completely different from whether it can be pointed.

Note 2: Note: Java provides a buffer pool mechanism for the String type. When using double quotes to define an object, the Java environment first goes to the string buffer pool to find a string with the same content, if it exists, use it, otherwise create a new one The string is placed in the buffer pool.

problem:

How to convert the numbers in the char array to int type

With the help of the wrapper class: Integer.valueOf(String.valueOf(numberArray[i]));

Guess you like

Origin blog.csdn.net/qq_45175218/article/details/104290491