Byte character string string comparison, find, replace, split, intercept

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


Preface

The String class in Java is a more commonly used class. Here are some commonly used functions in the String class.

1. Characters and strings

1. Character array -> string

    public static void main(String[] args) {
    
    
        char[] ch={
    
    'h','e','l','l','o'};
        String s1= new String(ch);
        System.out.println(s1);
    }

2. Turn part of the character array into a string

        char[] ch={
    
    'h','e','l','l','o'};
        String s1= new String(ch);
        System.out.println(s1);
        String s2=new String(ch,0,2);
        System.out.println(s2);

The result of running the code is as follows
Insert picture description here
3. Take the character at the specified position in the string

System.out.println(s1.charAt(1));

4. Turn the string into a character array and return

char[] chars=s1.toCharArray();

2. Bytes and strings

1. Byte array becomes string

        byte[] bytes={
    
    'w','o','r','l','d'};
        String s1=new String(bytes);

2. Return the string as a byte array

byte[] by=s1.getBytes();

3. String comparison

1. Case-sensitive comparison (Boolean)

System.out.println(s1.equals(s2));

2. Case-insensitive comparison (Boolean)

System.out.println(s1.equalsIgnoreCase(s2));

3. Compare the size relationship between two characters (int)

System.out.println(s1.compareTo(s2));

4. String search

1. Determine whether a substring s2 exists (boolean)

System.out.println(s1.contains(s2));

2. Search for the specified string s2 from the beginning, and find the start subscript of the return position, if it is not found, it will be -1 (there can be two parameters, and the second can specify the start search position) (int)

System.out.println(s1.indexOf(s2));

3. Find the position of the substring from back to front, as above (int)

System.out.println(s1.lastIndexOf(s2));

4. Determine whether to start with a specified string (there can be two parameters, the second can specify the position for judgment) (boolean)

System.out.println(s1.startsWith(s2));

5. Determine whether to end with the specified string (boolean)

System.out.println(s1.endsWith(s2));

5. String replacement

1. Replace all specified content

s1.replaceAll("he",s2);

2. Replace the first content

s1.replaceFirst("he",s2);

6. String split

Split all strings (there can be two parameters, the second parameter is the array length limit)

        String[] strings=s1.split(" ");
        String[] strings1=s1.split(" ",2);
        System.out.println(Arrays.toString(strings));
        System.out.println(Arrays.toString(strings1));

Insert picture description here

7. String interception (String)

Intercept from the specified index to the end (can have two parameters, the second parameter is the end position of the interception, left closed and right open)

        s2=s1.substring(2);
        System.out.println(s2);
        s2=s1.substring(2,4);
        System.out.println(s2);

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/112907678