类String的学习2

String 类代表字符串.
下面通过实例学习:

public class ToString2 {

    public static void test1(){
        String s="abcdefg";
        //将此 String 中的所有字符都转换为大写
        String upperCase = s.toUpperCase();
        System.out.println(upperCase);
    }

    public static void test2(){
        String s="ABCDFG";
        //将此 String 中的所有字符都转换为小写
        String lowerCase = s.toLowerCase();
        System.out.println(lowerCase);
    }

    public static void test3(){
        String s1="ABCD";
        String s2="xyz";
        String s4="结尾加在一起";
        //将指定字符串连接到此字符串的结尾
        String s3 = s1.concat(s2).concat(s4);
        System.out.println(s3);
    }

    public static void test4(){
        String s="abcd";
        byte[] bytes = s.getBytes();
        //将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
        for(int i=0;i<bytes.length;i++) {
            System.out.print(bytes[i]+" ");
        }
        System.out.println(" ");
    }

    public static void test5(){
        String s="abcd";
        char[] charArray = s.toCharArray();
        //将此字符串转换为一个新的字符数组。 
        for(int i=0;i<charArray.length;i++) {
            System.out.print(charArray[i]+" ");
        }
        System.out.println(" ");
    }

    public static void test6(){
        int i=888;
        //返回 int 参数的字符串表示形式。
        String intString = String.valueOf(i);
        System.out.println(intString);
    }

    public static void test7(){
        String s1="abcdefg1234dfwwwww";
        //返回一个新的字符串,从指定索引处的字符开始,直到此字符串末尾。
        String s2=s1.substring(3);
        System.out.println(s2);
    }

    public static void test8(){
        String s1="abcdefghi1234567";
        //从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符
        String s2=s1.substring(1,4);
        System.out.println(s2);
    }

    public static void test9(){
        String s1="       abcdefghi   1234567      ";
        System.out.println(s1.length());
        //返回字符串的副本,忽略前导空白和尾部空白,中部不能忽略
        String s2 = s1.trim();
        System.out.println(s2.length());
        System.out.println(s2);
    }

    public static void test10(){
        String s="hallo";
        //返回一个新的字符串,返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
        String s2 = s.replace('a', 'e');
        System.out.println(s2);
        String s3 = s.replace('l', 'v');
        System.out.println(s3);
    }

    public static void test11(){
        String s="hello world";
        String replacedString = s.replace(" ", "");
        System.out.println(replacedString);
    }
    public static void test12(){
        //除去字符串中的"ab",输出结果
        String str = "abcdefabc123ab66ababba";
        int count = 0;
        int index = 0;
        //返回指定字符在此字符串中第一次出现处的索引
        while((index = str.indexOf("ab"))!=-1){
            count++;
            //返回一个新的字符串,该子字符串从指定索引处的字符开始,直到此字符串末尾。
            str = str.substring(index+2);
        }
        System.out.println(index);
        System.out.println(count);
    }

    public static void main(String[] args) {
        test1();
        System.out.println("-------");
        test2();
        System.out.println("-------");
        test3();
        System.out.println("-------");
        test4();
        System.out.println("-------");
        test5();
        System.out.println("-------");
        test6();
        System.out.println("-------");
        test7();
        System.out.println("-------");
        test8();
        System.out.println("-------");
        test9();
        System.out.println("-------");
        test10();
        System.out.println("-------");
        test11();
        System.out.println("-------");
        test12();
    }
}
运行结果:
ABCDEFG
-------
abcdfg
-------
ABCDxyz结尾加在一起
-------
97 98 99 100  
-------
a b c d  
-------
888
-------
defg1234dfwwwww
-------
bcd
-------
32
19
abcdefghi   1234567
-------
hello
havvo
-------
helloworld
-------
-1
5

每档一语:
其实世界上没有那么多的如果,有时候,我们一瞬间失去的东西就是永恒。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值