String class and class StringBuffer

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43545471/article/details/102732724

Initialization 1 String class

1.1 Use string constant to initialize a String object directly

package String;

public class Main{
    public static void main(String[] args) {
        String str1 = null;
        String str2 = "";
        String str3 = "abc";
    }
}

1.2 String String object constructor initializes

package String;
 
import javax.print.DocFlavor;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 = new String(); // 创建一个内容为空的字符串
        String str2 = new String("abc"); // 根据指定字符串内容创建对象
        char[] charArry = new char[] {'a', 'b', 'c'};
        String str3 = new String(charArry); // 根据指定字符数组创建对象
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str1+str2+str3); // 字符串连接
    }
}

2 String class of common operations

2.1 Basic Operation string

package String;

public class Main {
    public static void main(String[] args) {
        String s = "abcabcbacdba";
        System.out.println(s.length()); // 获取字符串长度
        System.out.println(s.charAt(0)); // 返回index位置上的字符
        System.out.println(s.indexOf('c')); // 返回此字符在字符串中第一次出现的位置
        System.out.println(s.lastIndexOf('c')); // 返回此字符在字符串中最后一次出现的位置
        System.out.println(s.indexOf("ab")); // 返回子字符串在字符串中第一次出现的位置
        System.out.println(s.lastIndexOf("ab")); // 返回子字符串在字符串中最后一次出现的位置
    }
}

2.2 the string conversion operation

package String;

public class Main {
    public static void main(String[] args) {
        String str = "java";
        String str1 = "PYTHON";
        char[] charArry = str.toCharArray(); // 字符串转换为字符数组
        for (int i = 0; i < charArry.length; i++) {
            System.out.print(charArry[i]);
            if (i != charArry.length-1) {
                System.out.print(",");
            }
        }
        System.out.println();
        System.out.println(String.valueOf(12)); // int类型转换为String类型
        System.out.println(str.toUpperCase()); // 变大写
        System.out.println(str1.toLowerCase()); // 变小写
    }
}

Alternatively and to space string operations 2.3

package String;

public class Main {
    public static void main(String[] args) {
        String src = "   http : //www.baidu.com    ";
        System.out.println(src.trim()); // 去掉两端空格
        System.out.println(src.replace(" ", "")); // 将字符串中全部空格替换
    }
}

2.4 determination operation string

package String;

public class Main {
    public static void main(String[] args) {
        String S1 = "starter";
        String s2 = "st";
        System.out.println(S1.startsWith("st")); // 判断字符串是否以字符串st开头
        System.out.println(S1.endsWith("er")); // 判断字符串吗是否以字符串er结尾
        System.out.println(S1.contains("ar")); // 判断字符串是否包含字符串ar
        System.out.println(S1.isEmpty()); // 判断字符串是否为空
        System.out.println(S1.equals(s2)); // 判断两个字符串是否相等
    }
}

package String;

public class Main {
    public static void main(String[] args) {
        String str1 = new String("abc");
        String str2 = new String("abc");
        System.out.println(str1 == str2); // 判断两个字符串对象的地址是否相同
        System.out.println(str1 == "abc"); 
        System.out.println(str1.equals(str2)); // 判断两个字符串是否相等
    }
}

2.5 strings taken and divided

package String;

public class Main {
    public static void main(String[] args) {
        String str = "2018-01-24";
        System.out.println(str.substring(5)); // 从第六个字符截取到末尾
        System.out.println(str.substring(5,7)); // 从第六个截取到第七个
        String[] strArry = str.split("-"); // 通过“-”分割字符串为字符串数组
        for (int i = 0; i < strArry.length; i++) {
                System.out.print(strArry[i]);
                if (i != strArry.length-1) {
                    System.out.print(",");
                }
        }
    }
}

Note: When accessing character, there is no occurrence of an IndexOutOfBoundsException index

3 StringBuffer类

String class is final class, String defined character is a constant, so he once created, its content and length are immutable. If a string of modifications can only create a new string.
The maximum difference between StringBuffer class String class and its content and length that can be changed. StringBuffer character similar to a container, the container is operated, it will not generate a new object.

package String;

import javax.sound.midi.Soundbank;

public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("abc"); // 末尾追加字符
        System.out.println(sb);

        sb.insert(2, "de"); // 指定位置插入字符串
        System.out.println(sb);

        sb.setCharAt(2, 'c'); // 修改指定位置字符
        System.out.println(sb);

        sb.replace(2,5,"xyz"); // 替换指定位置字符串或字符
        System.out.println(sb);

        sb.reverse(); // 翻转字符串
        System.out.println(sb);

        sb.delete(0, 3); // 指定范围删除
        System.out.println(sb);
        
        sb.deleteCharAt(0); // 指定位置删除
        System.out.println(sb);
        
        sb.delete(0, sb.length()); // 清空缓冲区
        System.out.println(sb);
    }
}

NOTE:
1.String Object class overrides the method of equal tired, StringBuffer class does not override the equals method of Object class. (The equals method of Object class is determined whether the same object, and "==" same)
2.String class object may be "+" to connect, and the object is not StringBuffer class.

Guess you like

Origin blog.csdn.net/qq_43545471/article/details/102732724