Java学习----字符串函数

1.String

string类是最终类,不可以有子类

package com.demo.pkg1;

public class TestString {
    public static void main(String[] agrs) {
        // 构造字符串
        String name = "  hello world  ";
        String s1 = new String("aaa");
        
        // 长度
        int x = name.length();
        // 字符位置
        char c = name.charAt(3);
        // 子串
        String s = name.substring(5,8);
        // 去掉空格
        String ss = name.trim();
        
        System.out.println(x);
        System.out.println(c);
        System.out.println(s);
        System.out.println(ss);
    }
}

 15 e lo hello world 

2.StringBuffer(可变)

public class TestString {
    public static void main(String[] agrs) {
        StringBuffer s = new StringBuffer("hello");
        s.append(" world"); // string类型没有append
        System.out.println(s);
        System.out.println(s.toString()); // 转成string
    }
}
hello world

3.StringBuilder

StringBuilder线程不安全,StringBuffer线程安全。单线程使用StringBuilder,可以更快一点。

原文地址:https://www.cnblogs.com/dragon1013/p/5126076.html