java explanation -- some practical methods of string

Keywords: Java ascii

String is a class provided by the system, represented by string
Here are some common methods about String for your reference:

1. Access

It can be divided into two types: finding the charat in the string according to the index and finding the index of the character in the string according to the position of the character in the string

public static void fun1(){
    // Find the characters in the string according to the index
    String string = "javaxjjava";
    char index = string.charAt(4);
    System.out.println(index);// Output x
    // Find the index based on the position of the characters in the string
    // Find a single character directly
    int index1 = string.indexOf("a");
    System.out.println(index1);// Output is 1
    // Find a single character starting at
    int index2 = string.indexOf("a",2);
    System.out.println(index2);// Output 3
    // Find multiple characters directly
    int index3 = string.indexOf("va");
    system.out.println(index3);// Output 2
    // Start with the first few characters
    int index4 = string.indexOf("va",4);
    System.out.println(index4);// Output 8

2. Judgment

There are three types: judge whether it starts with this prefix (startWith), judge whether it ends with this suffix (endWith), and judge whether the package does not contain this character (contains)

public static void fun2() {
    String string = "javaxjjava";
    // Judging prefix
    boolean index1 = string.startsWith("ja");
    System.out.println(index1);// Output is true
    // Judgment contains a character
    boolean index2 = string.contains("xj");
    System.out.println(index2);// Output is true
    // Determine suffix
    boolean index3 = string.endsWith("va");
    System.out.println(index3);// Output is true

3. Replacement

Using the replace method

public static void fun3() {
    String string = "javaxjjava";
    String index = string.replace("xj", "jx");
    // The output is javajxjava

4. Cutting

Using the spill method

public static void fun4() {
    String string = "javaxjjava";
    int index = string.spilt("x");
    // Enhance for loop
    // The element of the array is placed before the colon, and the array to be traversed is placed after the bubble
    for(String index : spilt){
        // The output is java jjava

5. Get string

Use substring method

public static void fun5() {
    String string = "javaxjjava";
    // Select a character to get the string after including this character
    String index1 = String.substring(4);
    System.out.println(index1);// The output is xjjava
    // Get the string of an interval
    //[head without tail)
    String index2 = string.substring(4,6);
    System.out,println(index2);// The output is xj

6. Convert case

Lowercase to uppercase with toSupperCase method
Use toLowerCase method to convert upper case to lower case

public static void fun6() {
    String string = "javaXJjava";
    // Lowercase to uppercase
    String index1 = string.toSupperCase();
    // The output is JAVAXJJAVA
    // Capitalize to lowercase
    String index2 = string.toLowerCase();
    // The output is javaxjjava

7. Judge that two strings are equal (ignore case)

Using the equalsIgnoreCase method

public static void fun7() {
    String string1 = "javaXJjava";
    String string2 = "JAVAxjJAVA";
    boolean index = 
    System.out.println(index);// Output is true

8. Go to blank

Using the trim method

public static void fun8() {
    String string = "   java xj java   ";
    String trim = string.trim();
    // The output is java xj java

9. Comparison of two strings

Is the comparison of ASCII codes of two strings
Using the compareTo method

public static void fun9() {
    String string1 = "javaxjjava";
    String string2 = "javaxkjava";
    int index = string1.compareTo(string2);
    // Output is - 1

10. Transformation

There are two types:
Character array to string and string to character array

public static void fun10(){
    // Convert character array to string
    char[] array1 = new char[]{'j','a','v','a'};
    String index = new String(array);
    // The output is java
    // String to character array
    String string = "java";
    char[] array2 = string.toCharArray();
    for(char c : array){
        system.out.println(c + " ");
        // The output is j a v a

Posted by scorphus on Sat, 30 May 2020 08:39:20 -0700