String类使用案例:字符串替换、数组转化为字符串、String与其他数据类型相互转换、去除字符串中的空格

String使用案例
字符串中替换的方法

public class StringDemo {
    public static void main(String[] args) {
        //字符串中替换的方法
        String s = "奥巴马是美国总统".replace('马', '牛');
        System.out.println(s);
        String s1 = "奥巴马是美国总统".replace("奥巴马", "***");
        System.out.println(s1);
        String s2 = "奥巴马是美国总统特朗普也是美国总总统".replace("奥巴马", "****").replace("特朗普", "***");
        System.out.println(s2);
       //字符串去除两端空格的方法
        String trim = "    abc     ".trim();
        System.out.println(trim);

        //对比字符串
        //通过字典顺序去比较 返回的值是 ASCII 码的差值  调用者减去传入者
        int i = "abc".compareTo("Abc");
        System.out.println(i);
        //通过长度去比
        int i1 = "abc".compareTo("a");
        System.out.println(i1);

        //忽略大小写的比较
        int abc = "abc".compareToIgnoreCase("ABC");
        System.out.println(abc);


    }
}

数组中的数据按照指定个格式拼接成一个字符串

//A:案例演示
// 需求:把数组中的数据按照指定个格式拼接成一个字符串
// 举例:
// int[] arr = {1,2,3};
// 拼接结果:
// “[1, 2, 3]”

public class MyDemo3 {
    public static void main(String[] args) {
        String str = "[";
        int[] arr = {1, 2, 3};
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                str += arr[i] + "]";
            } else {
                str += arr[i] + ",";
            }
        }
        System.out.println(str);
    }
}

String的转换功能

//A:
//String的转换功能:
//public byte[] getBytes ():把字符串转换为字节数组。
//public char[] toCharArray ():把字符串转换为字符数组。
//public static String valueOf ( char[] chs):把字符数组转成字符串。
//public static String valueOf ( int i):把int类型的数据转成字符串。
//注意:String类的valueOf方法可以把任意类型的数据转成字符串。
//public String toLowerCase ():把字符串转成小写。
//public String toUpperCase ():把字符串转成大写。
//public String concat (String str):把字符串拼接。

案例一:

public class MyTest {
    public static void main(String[] args) {
        //B:
        //案例演示
        //        案例演示String类的转换功能

        //把字节数组转成字符串
        //String s = new String(new byte[]{100, 127},0,1);
        //System.out.println(s);

        byte[] bytes = "abcd".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        System.out.println(new String(bytes));

        byte[] bytes1 = "你好啊".getBytes();
        System.out.println(bytes1.length);

        for (int i = 0; i < bytes1.length; i++) {
            System.out.println(bytes1[i]);
        }

        System.out.println(new String(bytes1));

    }
}

案例二:

import java.net.SocketTimeoutException;

public class MyTest2 {
    public static void main(String[] args) {
        //把字符数组转换成字符串
        String s = new String(new char[]{'a', 'b', 'c'},0,2);
        System.out.println(s);



        String str="上课不要玩王者荣耀";
        //char[] chas=new char[str.length()];
        //for (int i = 0; i < str.length(); i++) {
        //    char c = str.charAt(i);
        //    chas[i]=c;
        //}
        //
        //把字符串转换成字符数组
        char[] chars = str.toCharArray();
        //for (char c : chars) {
        //    System.out.println(c);
        //}
        //转换大小写

        String s1 = "abcd".toUpperCase();
        System.out.println(s1);

        String s2 = "ABCD".toLowerCase();
        System.out.println(s2);

    }
}

任意类型转换成字符串

public class MyTest3 {
    public static void main(String[] args) {
        //把数字字符串
        int num = 100;

        String str = num + ""; //"100"

        boolean b = false;
        String str2 = b + ""; //"false"

        //把任意类型转换成字符串

        String s = String.valueOf(100);
        String s1 = String.valueOf(new char[]{'a', 'b'});
        String s2 = new String(new char[]{'a', 'b'});

        String s3 = String.valueOf(new Object());
        System.out.println(s3);


        String str3="abc"+"ccc"+"ddd";

        //concat("ccc") 拼接字符串,可以替代加号
        String concat = "abc".concat("ccc").concat("aaaa").concat("abbc");
        System.out.println(concat);
        "abc".concat("");


    }
}

把一个字符串的首字母转成大写,其余为小写

public class MyTest4 {
    public static void main(String[] args) {
        //A:
        //案例演示:
        //需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
        String str="aAFSDFDfdfadfasdfasdfasfdasdfasdf";
        String s = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
        System.out.println(s);
}

统计大串中小串出现的次数
案例:

//A:
//画图演示
//需求:统计大串中小串出现的次数
//举例:
//"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次

public class MyTest5 {
    public static void main(String[] args) {
        
        String maxStr="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        String minStr="java";

        //把一段代码抽取到一个方法中 ctrl+alt+M
        findStr(maxStr, minStr);

        //方式2============================
        int count=0;
        String newStr = maxStr.replace("java", "0");
        System.out.println(newStr);
        for (int i = 0; i < newStr.length(); i++) {
            char ch = newStr.charAt(i);
            if(ch=='0'){
                count++;
            }

        }

        System.out.println("Java出现的次数" + count + "次");


    }

    private static void findStr(String maxStr, String minStr) {
        int count=0;
        //查找java出现的索引
        int index= maxStr.indexOf(minStr);
        while (index!=-1){
            count++;
           maxStr= maxStr.substring(index+4);
           //再次改变索引
            index = maxStr.indexOf(minStr);
        }

        System.out.println("Java出现的次数"+count+"次");
    }
}

去除字符串的空格

public class MyTest8 {
    public static void main(String[] args) {
        String str = "   abc   ";
        //去除上面字符串的左端空格
        //思路:遍历字符串,截取每个字符,判断你不是空格 取出你不是空格字符的那个字符的索引
        String s = "";
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch != ' ') {
                int index = str.indexOf(ch);
                s = str.substring(index);
                break;
            }
        }
        System.out.println(s + "abc");//为了看出空格被删除
        String str2 = "   abc   ";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值