聊聊 String 的常用方法

先看 String 的底层源码:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

String 类代表字符串,字符串是常量 ,用双引号引起来表示。 它的值在创建之后不能更改 。

由final修饰,代表不可变的字符序列 ;
实现了序列化、Comparable接口和 CharSequence 接口;
String 对象的字符内容是存储在一个 char 型的数组中
常用方法:

int length() 返回此字符串的长度
char charAt(int index) 返回 char指定索引处的值
boolean isEmpty() 判断是否是空字符串
String toLowerCase() 将 String 中的所有字符转换为小写
String toUpperCase() 将 String 中的所有字符转换为大写
boolean equalsIgnoreCase(String anotherString) 判断是否相等,忽略大小写
boolean equals(Object obj) 比较字符串的内容是否相同
String trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格
String concat(String str) 将指定的字符串连接到该字符串的末尾
String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串
String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串
int compareTo(String anotherString) 按字典顺序比较两个字符串
测试代码:

public class StringMethodTest1 {
    public static void main(String[] args) {
        String s = "HelloWorld";
        // int length() 返回此字符串的长度
        System.out.println(s.length()); // 10

        // char charAt(int index) 返回 char指定索引处的值
        System.out.println(s.charAt(1)); // e

        // boolean isEmpty() 判断是否是空字符串
        System.out.println(s.isEmpty()); // false

        // String toLowerCase() 将 String 中的所有字符转换为小写
        System.out.println(s.toLowerCase()); // helloworld

        // String toUpperCase() 将 String 中的所有字符转换为大写
        System.out.println(s.toUpperCase()); // HELLOWORLD

        // boolean equalsIgnoreCase(String anotherString) 判断是否相等,忽略大小写
        String s1 = "HELLOworld";
        System.out.println(s.equalsIgnoreCase(s1)); // true

        // boolean equals(Object obj) 比较字符串的内容是否相同
        System.out.println(s.equals(s1)); // false

        String s2 = " HelloChina ";
        // String trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格
        System.out.println(s2.trim()); // HelloChina

        // String concat(String str) 将指定的字符串连接到该字符串的末尾
        System.out.println(s.concat(s2)); // HelloWorld HelloChina

        // String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串
        System.out.println(s.substring(5)); // world

        // String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串
        System.out.println(s.substring(5, 8)); // wor

        // int compareTo(String anotherString) 按字典顺序比较两个字符串
        System.out.println(s.compareTo(s1)); // 32

    }
}

代码执行结果:

10
e
false
helloworld
HELLOWORLD
true
false
HelloChina
HelloWorld HelloChina 
World
Wor
32

boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true
int indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引
int indexOf(String str, int fromIndex) 返回指定子串的第一次出现的字符串中的索引,从指定的索引开始
int lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引
int lastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始反向搜索
boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头
boolean startsWith(String prefix, int toffset) 测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头
boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾
测试代码:

public class StringMethodTest2 {
    public static void main(String[] args) {
        String s = "HelloWorld";

        // boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true。
        System.out.println(s.contains("or")); // true

        // int indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引
        System.out.println(s.indexOf("ll")); // 2

        // int indexOf(String str, int fromIndex) 返回指定子串的第一次出现的字符串中的索引,从指定的索引开始
        System.out.println(s.indexOf("o", 5)); // 6

        // int lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引。
        System.out.println(s.lastIndexOf("o")); // 6

        // int lastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始反向搜索。
        System.out.println(s.lastIndexOf("o", 5)); // 4

        // boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头
        System.out.println(s.startsWith("He")); // true

        // boolean startsWith(String prefix, int toffset) 测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头
        System.out.println(s.startsWith("W", 5)); // true

        // boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾
        System.out.println(s.endsWith("ld")); // true

    }
}

代码执行结果:

true
2
6
6
4
true
true
true

String replace(char oldChar, char newChar) 字符串替换,返回一个新的字符串
String replace(CharSequence target, CharSequence replacement) 将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列
boolean matches(String regex) 告诉这个字符串是否匹配给定的 regular expression
String[] split(String regex) 将此字符串分割为给定的 regular expression的匹配
测试代码:

public class StringMethodTest3 {
    public static void main(String[] args) {

        String s = "HelloWorld";

        // String replace(char oldChar, char newChar) 字符串替换,返回一个新的字符串
        System.out.println(s.replace("H", "A")); // AelloWorld

        // String replace(CharSequence target, CharSequence replacement)
        // 将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。
        String s1 = s.replace("World", "China");
        System.out.println(s); // HelloWorld
        System.out.println(s1); // HelloChina

        // boolean matches(String regex) 告诉这个字符串是否匹配给定的 regular expression
        String s2 = "123,456";
        boolean matches = s2.matches("\\d+");
        System.out.println(matches); // false

        // String[] split(String regex) 将此字符串分割为给定的 regular expression的匹配。
        String[] split = s2.split(",");

        for (String str : split) {
            System.out.print(str + " "); // 123 456
        }

    }
}

代码执行结果:

AelloWorld
HelloWorld
HelloChina
false
123 456
更多Java学习资料可关注:gzitcast获取

发布了782 篇原创文章 · 获赞 3 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/u010395024/article/details/104959956
今日推荐