java字符串简单操作

来源:互联网 发布:杀破狼js mp3下载 编辑:程序博客网 时间:2024/06/06 00:43
1、找出字符串中包含另一个字符或者字符串的个数
   static int charNum(String str,char ch) {        int count = 0;        for(int i=0;i<str.length();i++){        if (str.charAt(i)== ch) {        count++;        }        }return count; }  static int strNum(String str,String s) {        int count = 0;        int index =str.indexOf(s);        while(index!=-1){          count++;              str=str.substring(index+s.length());              System.out.println(str);              index=str.indexOf(s);        }return count;}
2、把一个字符串中某个字符都去掉
static String squeezeOut(String from, char toss){   char[ ] chars = from.toCharArray();   int len = chars.length;   int put = 0;   for(int i=0; i<len;i++){     if(chars[i] != toss)       char[put++] = chars[i];   }  return new String(chars,0,put);}






0 0
原创粉丝点击