String类的常见方法的使用案例
发布日期:2021-08-31 13:57:46 浏览次数:27 分类:技术文章

本文共 1322 字,大约阅读时间需要 4 分钟。

 

 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/YLQBL/p/6413722.html

转载地址:https://blog.csdn.net/weixin_34077371/article/details/93150949 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:我对OpenAPI的理解
下一篇:设计模式:依赖倒转原则(记录一)

发表评论

最新留言

不错!
[***.144.177.141]2024年03月28日 21时14分14秒