跪求大虾

kobe_gino 2010-05-02 09:16:25
怎样判断一个字符串中某个字符出现的次数??????
...全文
237 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤鸿掠影 2011-03-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 syilo 的回复:]
Java code

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "dfeanciaadec";
int count = 0;
for(int i = 0;i<(str.length() ……
[/Quote]

比较推荐这种算法。。。

Ganymede 2011-03-24
  • 打赏
  • 举报
回复
大家用split方法验证过么?10楼的方法经过验证了,无论是字符串还是字符都没问题。
y406719497 2011-03-24
  • 打赏
  • 举报
回复
split方法
xccloveny 2011-03-24
  • 打赏
  • 举报
回复
indexof() 吧
snaillxc 2011-03-24
  • 打赏
  • 举报
回复
正则表达式。。。
JamesLiu 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 hudie1234567 的回复:]
下面这个方法可以统计一个字符串中某个字符或字符串出现的次数,代码如下:

Java code

public int search(String str, String find) {//返回的就是find字符串在str中出现的次数
String[] findStr = str.split(find);
return findStr.length;
}
[/Quote]

正解,也是最简单的
zhuosh 2011-03-24
  • 打赏
  • 举报
回复
考虑使用循环
xiaqiang224 2010-05-10
  • 打赏
  • 举报
回复
基本上差不多
zhuyouyong 2010-05-10
  • 打赏
  • 举报
回复
顶[Quote=引用 3 楼 whut_lcy 的回复:]
比如
String source = ... //源字符串
String substr = ... //子串
String dest = ... //你要查找的字符串
int index = 0; //字符串首次出现的位置
int count = 0; //字符串出现的总次数

substr = source;

while(substr != "" && (……
[/Quote]
sunwei0615 2010-05-09
  • 打赏
  • 举报
回复
correction


String s = "alsdkjflaskjdf;lahwejrlksadjflaksdjfljasld;f";
String [] ss = s.split("a");

System.out.println("String \\"a\\" appears "+ ss.length-1 +" times");
sunwei0615 2010-05-09
  • 打赏
  • 举报
回复
String s = "alsdkjflaskjdf;lahwejrlksadjflaksdjfljasld;f";
String [] ss = s.split("a");

System.out.println("String \\"a\\" appears "+ss.length+" times");
kobe_gino 2010-05-09
  • 打赏
  • 举报
回复
xie xie
Jay_xiaolei 2010-05-08
  • 打赏
  • 举报
回复
用split方法
eve_zz 2010-05-08
  • 打赏
  • 举报
回复
其实一楼的字符串split方法就蛮好的啊。
茫茫大海 2010-05-08
  • 打赏
  • 举报
回复
下面这个方法可以统计一个字符串中某个字符或字符串出现的次数,代码如下:

public int search(String str, String find) {//返回的就是find字符串在str中出现的次数
String[] findStr = str.split(find);
return findStr.length;
}

ZHXDD 2010-05-08
  • 打赏
  • 举报
回复
String reg = "a";
String str = "abcadddfa";
Pattern p = Pattern.compile(reg);
java.util.regex.Matcher m = p.matcher(str);
int count = 0;
while (m.find()) {
count++;
}
System.out.println(count);
csupanmin 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 cwjieno1 的回复:]
Java code

public static int count(String str,char ch){
int count = 0;
for(int i=0;i<str.length();i++){
count = (str.charAt(i)==ch)?count+1:count;
}
return count;
}
[/Quote]
这是使用INdexOF 来查找字符的
kobe_gino 2010-05-04
  • 打赏
  • 举报
回复
thanks
cwjieNo1 2010-05-04
  • 打赏
  • 举报
回复

public static int count(String str,char ch){
int count = 0;
for(int i=0;i<str.length();i++){
count = (str.charAt(i)==ch)?count+1:count;
}
return count;
}
syilo 2010-05-04
  • 打赏
  • 举报
回复

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "dfeanciaadec";
int count = 0;
for(int i = 0;i<(str.length() - 1);i++){
if('a' == (str.charAt(i))){
System.out.println("Hello,world!");
System.out.println("i=" + i);
count++;
System.out.println("count=" + count);
continue;
}
}
}
加载更多回复(10)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧