String类的常见方法的使用案例

 

 String类的常见方法的使用案例

        //使用指定的字符串替换当前字符串中指定的内容
        //将helloworld中的o替换为a
        String s="HelloWorld";
        String ss=s.replace("o","a");
        System.out.println("替换前:"+s);
        System.out.println("替换后:"+ss);
        
        //截取当前字符串中指定的内容,保留HelloWorld中0-w之间的内容,不包括w,s.indexOf("w")表示w在字符串s中的下标,下标是从0开始的;
        s=s.substring(0, s.indexOf("W"));
        System.out.println("截取以后的值:"+s);
        
        //将字符串转为大写或者小写
        System.out.println("将s变量中包含的字符串转为小写:"+s.toLowerCase());
        System.out.println("将s变量中包含的字符串转为大写:"+s.toUpperCase());
        
        //忽略大小写的比较
        System.out.println("忽略大小写的比较::"+"Hello".equalsIgnoreCase("HELLO"));

 

算当前字符串中有多少个指定字符的案例

public static void main(String[] args){
        
        String s1="1110001110111111asdas1111csdcsdc" +
                "222111111111111111111121222";
        int totol=count1(s1, "1");
        System.out.println(totol);
    }
public static int count1(String s1,String s2){ String s=s1; int count =0; while(true){ if (s.indexOf(s2)!=-1) {//如果找到s2 count++; s=s.substring(s.indexOf(s2)+1);//将第一次找到的dest截掉,保留后面的部分,继续查找 continue ; }else{ break; } } return count; }

 

转载于:https://www.cnblogs.com/xh_Blog/p/6413722.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值