Fork me on GitHub

【JAVA笔记】JAVA常用的字符串操作03

一、Java中常用的字符串操作

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

        boolean p1 = isEmpty("aa");
        System.out.println("判断字符串是否为空:" + p1);
        String p2 = subStartString("a1234567890", "a", "3");
        System.out.println("判断字符串start开始,end结束之间的内容:" + p2);
        String p3 = subStartString1("abcdef", "c", 2);
        System.out.println("判断字符串start开始的后几位:" + p3);
        String p4 = insertString("abc", "f", 2);
        System.out.println("index字符位插入insert字符:" + p4);
        boolean p5 = contains("abcdef", "ef");
        System.out.println("判断str是否包含subStr:" + p5);
        boolean p6 = containsIgnoreCase("ABCdef", "bc");
        System.out.println("判断str是否包含subStr(不区分大小写):" + p6);
        boolean p7 = startWith("abcdef", "abc");
        System.out.println("判断字符串是否以start开头:" + p7);
        boolean p8 = endWith("abcdef", "ef");
        System.out.println("判断字符串是否以end结尾:" + p8);
        String p9 = replace("abcdef", "de", "ff");
        System.out.printf("字符串替换:" + p9);
        boolean p10 = equals("11", "cc");
        System.out.println("判断字符串是否相同:" + p10);
        boolean p11 = equalsIgnoreCase("AABBCC", "aabbcc");
        System.out.println("判断字符串是否相同(忽略大小写):" + p11);
        String p12 = toUpperCase("bbccdf");
        System.out.println("字符串小写变大写:" + p12);
        String p13 = toLowerCase("AAccdf");
        System.out.println("字符串大写变小写:" + p13);
        int p14 = length("aaaaaabbbbbbbbbbccccccccc");
        System.out.println("统计字符串长度:" + p14);

    }

    //1、判断字符串是否为空
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    //2、截取字符串(star起,end闭之间的内容)
    public static String subStartString(String str, String start, String end) {
        int startIndex = str.indexOf(start);
        int endIndex = str.indexOf(end);
        return str.substring(startIndex, endIndex);
    }

    //3、截取字符串 start开始,count后几位之间的字符串
    public static String subStartString1(String str, String start, int count) {
        int startIndex = str.indexOf(start);
        return str.substring(startIndex + start.length(), startIndex + start.length() + count);
    }


    //  4、插入字符串
    public static String insertString(String str, String insert, int index) {
        return str.substring(0, index) + insert + str.substring(index);
    }

    //5、是否包含字符串
    public static boolean contains(String str, String subStr) {
        return str.indexOf(subStr) != -1;
    }

    // 6、是否包含字符串不区分大小写
    public static boolean containsIgnoreCase(String str, String subStr) {
        return str.toLowerCase().indexOf(subStr.toLowerCase()) != -1;
    }


    //7、是否以字符串开头
    public static boolean startWith(String str, String start) {
        return str.startsWith(start);
    }

    //8、是否以字符串结尾
    public static boolean endWith(String str, String end) {
        return str.endsWith(end);
    }

    //9、替换字符串
    public static String replace(String str, String oldStr, String newStr) {
        return str.replace(oldStr, newStr);
    }

    //10、比较字符串-是否相等
    public static boolean equals(String str1, String str2) {
        return str1.equals(str2);
    }

    //11、比较字符串,忽略大小写
    public static boolean equalsIgnoreCase(String str1, String str2) {
        return str1.equalsIgnoreCase(str2);
    }

    //12、字符串大写
    public static String toUpperCase(String str) {
        return str.toUpperCase();
    }

    //13、字符串小写
    public static String toLowerCase(String str) {
        return str.toLowerCase();
    }

    //14、字符串长度
    public static int length(String str) {
        return str.length();
    }


}

运行结果:  

判断字符串是否为空:false
判断字符串start开始,end结束之间的内容:a12
判断字符串start开始的后几位:de
index字符位插入insert字符:abfc
判断str是否包含subStr:true
判断str是否包含subStr(不区分大小写):true
判断字符串是否以start开头:true
判断字符串是否以end结尾:true
字符串替换:abcfff判断字符串是否相同:false
判断字符串是否相同(忽略大小写):true
字符串小写变大写:BBCCDF
字符串大写变小写:aaccdf
统计字符串长度:25

 

参考文章:

https://www.weixueyuan.net/view/6317.html  

posted @ 2022-11-24 17:15  橘子偏爱橙子  阅读(28)  评论(0编辑  收藏  举报