欢迎访问宙启技术站
智能推送

Java字符串处理函数:如何调用常用的字符串处理函数,如toUpperCase和toLowerCase等?

发布时间:2023-05-23 16:58:47

Java中提供了很多字符串处理函数,其中包含了一些常用的函数,如字符串转换为大写字母或小写字母,字符串查找、替换、拆分等功能。下面我们来介绍如何调用Java常用的字符串处理函数。

1. toUpperCase和toLowerCase函数

toUpperCase函数可以将字符串中的所有字母转换为大写字母,而toLowerCase函数则可以将字符串中的所有字母转换为小写字母。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello World";
        System.out.println("Original String: " + s1);
        String s2 = s1.toUpperCase();
        System.out.println("Uppercase String: " + s2);
        String s3 = s1.toLowerCase();
        System.out.println("Lowercase String: " + s3);
    }
}

运行结果为:

Original String: Hello World
Uppercase String: HELLO WORLD
Lowercase String: hello world

2. equals和equalsIgnoreCase函数

equals函数用于比较两个字符串是否相等,而equalsIgnoreCase函数则忽略字符串中的大小写字母比较。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = "hello world";
        String s3 = "HELLO WORLD";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println(s1.equalsIgnoreCase(s3));
    }
}

运行结果为:

false
true
true

3. indexOf和lastIndexOf函数

indexOf函数用于查找某个子字符串在字符串中 次出现的位置,而lastIndexOf函数则用于查找某个子字符串在字符串中最后一次出现的位置。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello World";
        int index1 = s1.indexOf("e");
        int index2 = s1.lastIndexOf("o");
        System.out.println("Index of 'e': " + index1);
        System.out.println("Last index of 'o': " + index2);
    }
}

运行结果为:

Index of 'e': 1
Last index of 'o': 7

4. replace和replaceAll函数

replace函数用于替换字符串中的某个子字符串,而replaceAll函数则用于替换字符串中的所有匹配的子字符串。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = s1.replace("World", "Java");
        System.out.println("Replaced String: " + s2);
        String s3 = "Hello Java World";
        String s4 = s3.replaceAll("Java", "Python");
        System.out.println("Replaced all String: " + s4);
    }
}

运行结果为:

Replaced String: Hello Java
Replaced all String: Hello Python World

5. split函数

split函数用于将字符串按照某个分隔符进行拆分并返回一个字符串数组。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello,World,Java";
        String[] a1 = s1.split(",");
        for (int i = 0; i < a1.length; i++) {
            System.out.println(a1[i]);
        }
    }
}

运行结果为:

Hello
World
Java

6. trim函数

trim函数用于去除字符串中的前导空格和尾随空格。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "  Hello World  ";
        String s2 = s1.trim();
        System.out.println(s2);
    }
}

运行结果为:

Hello World

7. length函数

length函数用于获取字符串的长度(即字符数)。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello World";
        int len = s1.length();
        System.out.println("Length of string: " + len);
    }
}

运行结果为:

Length of string: 11

8. startsWith和endsWith函数

startsWith函数用于判断字符串是否以某个子字符串开头,而endsWith函数则用于判断字符串是否以某个子字符串结尾。示例代码如下:

public class StringExamples {
    public static void main(String[] args) {
        String s1 = "Hello World";
        boolean b1 = s1.startsWith("Hello");
        boolean b2 = s1.endsWith("World");
        System.out.println("Starts with 'Hello': " + b1);
        System.out.println("Ends with 'World': " + b2);
    }
}

运行结果为:

Starts with 'Hello': true
Ends with 'World': true

总而言之,在Java中,字符串处理函数非常丰富,可以根据具体需求选择合适的函数来处理字符串。上述介绍的函数只是其中一部分,更多的字符串处理函数可以查看Java官方文档。