以下是我写的一个具体的实现,可以把同一个字符的个数(包括大小写)都进行统计. class PrintingContainer { public int searchString(String str,String s) { int i=0; int count=0; while(i>=0) { i=str.indexOf(s,i); if(i>=0) { i++; count++; } } return count; } public static void main(String[] args) {
String str= "afdadfafDADFADFXFDAFDCXXSSSFDSFADFSSSssssssfdsfdsfc"; String s = "S"; PrintingContainer p = new PrintingContainer(); int count1 = p.searchString(str,s); int count2 = p.searchString(str,s.toLowerCase()); int totalCount = count1 + count2;//字符的总个数 System.out.println(totalCount); } }
你用int indexof(String str,int startIndex),明白没
后面的startindex是你查找的起点,先取出字符串的长度,然后可以做循环,改变这个起点,当找到了的时候,即不是返回"-1",你就计数
明白了吧
int count=0;
while(i>=0){
i=indexOf(string,i); //string 被查询的字符串,i 查找的起始点,从0开始查找循环一直到往下查找,直到找不到位置
if(i>=0){
i++; //如果找到该字符串,将查找起始地址下一一位
count++; //查找的数量加一
}
}
i=str.indexOf(string,i);
str被查询的字符串,string要查询的字符串
建议你去下个javadoc看看,
在用得过程中不断得查查。
char ch='g';
char[] c=str.toCharArray();
int j=0; //出现的次数
for(int i=0;i<c.length;i++){
if(ch==c[i])
j++;
}
class PrintingContainer
{
public int searchString(String str,String s)
{
int i=0;
int count=0;
while(i>=0)
{
i=str.indexOf(s,i); if(i>=0)
{
i++;
count++;
}
}
return count; }
public static void main(String[] args)
{
String str= "afdadfafDADFADFXFDAFDCXXSSSFDSFADFSSSssssssfdsfdsfc";
String s = "S";
PrintingContainer p = new PrintingContainer();
int count1 = p.searchString(str,s);
int count2 = p.searchString(str,s.toLowerCase());
int totalCount = count1 + count2;//字符的总个数
System.out.println(totalCount); }
}
/// org.apache.commons.lang.StringUtils.countMatches("","");
public static int countMatches(String str, String sub) {
if (str==null || sub=null) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != -1) {
count++;
idx += sub.length();
}
return count;
}
public static int countMatches(String str, String sub) {
if ((str == null) || (sub == null)) {
return 0;
}
return str.length()-str.replaceAll(sub,"").length();
}
if ((str == null) || (sub == null)) {
return 0;
}
return str.length()-str.replaceAll(sub,"").length();
}
方法好但是偏离主题要求了
int i=0;
int count=0;
while(i>=0){
i=indexOf(string,i); //string 被查询的字符串,i 查找的起始点,从0开始查找循环一直到往下查找,直到找不到位置
if(i>=0){
i++; //如果找到该字符串,将查找起始地址下一一位
count++; //查找的数量加一
}
}
回答对题
String findstring = "oo";
int i = 0;
int count = 0;
while ((i = str.indexOf(findstring, i)) != -1) {
count++;
i += findstring.length();
}
System.out.println(count);
“itjourney(IT之旅)”的方法,当sub不是单个字符时,应该除以sub.length
return (str.length()-str.replaceAll(sub,"").length())/sub.length;“weinickli(weili)”的方法,还要减1。而且当str在最后几位出现findstring时,str不会再拆分,所以结果不正确。应该使用String.split(String regex, int limit)方法
int count = str.split(findstring, -1).length - 1;
对于单个字符查找,象“vgvg”那样直接遍历字符串不好吗?为什么要搞那么复杂?String str = "abcdabcaa";
char ch = 'a';
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (ch == str.charAt(i)) {
count++;
}
}
System.out.println(count);
呵呵你说的我改正了~~不过不知道为什么java的String.split对有的字符不能正确分解
能不能解释下
split(String regex, int limit)的第1个参数regex是正则表达式,而有一些字符,如:|, (, ), {, }, [, ], ?, +, *, ^, $, .,……等等在正则表达式中是有特殊作用的。如果要匹配上述字符,需要使用正则表达式中的“\”字符进行转义,如正则表达式“\|”就表示字符“|”。另外,我们如果使用Java字符串字面常量表示字符串“\|”时,要使用"\\|",这是因为“\”是Java语言的字符串字面常量(string literals)的转义字符,要表示字符“\”,需要使用"\\"。如下:
String regex = "\\|"; //字符串str在内存中的实际内容为“\|”,而“\|”用于正则表达式则可以匹配“|”这个字符
String[] arr = string.split("\\|"); //匹配“|”也可以使用\0nn,\xhh,\uhhhh的形式匹配任何字符,如:
String[] arr = string.split("\\x7c"); //匹配“|”
String regex = "\\|"; //字符串regex在内存中的实际内容为“\|”,而“\|”用于正则表达式则可以匹配“|”这个字符
String[] arr = string.split("\\|"); //表示“\|”,匹配“|”也可以使用\0nn,\xhh,\uhhhh的形式匹配任何字符,如:
String[] arr = string.split("\\x7c"); //表示“\|”,匹配“|”
转化小写函数toLowerCase(),String自带
谢谢指教了~~~````进我的贴~给分二十~~以谢指教