找出String中有多少个子String,有没有什么好办法.比如: String得到中有多少个"," 谢谢!!!

MPU 2004-12-13 11:06:29
找出String中有多少个子String,有没有什么好办法.

比如: String得到中有多少个","

谢谢!!!

比如: “,,,,,,” 用下列方法都不可靠…


String[] Ret = cs.splitCSV(",");
System.out.println( Ret.length );

StringTokenizer stToken = new StringTokenizer(strdoc,",");
System.out.println( stToken.countTokens() );

...全文
283 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
treeroot 2004-12-15
  • 打赏
  • 举报
回复
static int count(String str,String sub){
int index=str.indexOf(sub);
int len=sub.length();
int cnt=0;
while (index!=-1)
{
index+=len;
index=str.indexOf(sub,index);
cnt++;
}

return cnt;
}
MPU 2004-12-15
  • 打赏
  • 举报
回复
String strFindSub = "232"; // 要找的Substring...
String s1 = "1,,223,42,56,22";
int strs = ( s1.length()-s1.replaceAll(strFindSub,"").length() ) / strFindSub.length();
System.out.println( strs );

一般情况下这样估计就可以了...

但是如果我要找 "\\" , "\" .....等

怎么办............
MPU 2004-12-15
  • 打赏
  • 举报
回复
如果找",," 就不对了.

String s1 = "1,,2,3,4,5,,6";
int strs = s1.length()-s1.replaceAll(",,","").length();
System.out.println( strs );
catblue 2004-12-14
  • 打赏
  • 举报
回复
String s1 = ",,,,,";
int strs = s1.length()-s1.replaceAll(",","").length();
这个最好了,我看。
sandsworlf 2004-12-14
  • 打赏
  • 举报
回复
static String a = "hellohellohello";
System.out.println(a.replaceAll("el","le"));
JackyYin 2004-12-14
  • 打赏
  • 举报
回复
我应聘时的一道题目
有一string a
还有一string b
其中b包含在a里面 据个例子 a="hello" b="el"
要求 遍历 a字符串 然后把 a中所有的 b 全部反过来
再举个 例子 a="hello" b="el" a中有一个b 那么就把el换为 le 最后输出 a="hlelo"
IEQQ 2004-12-14
  • 打赏
  • 举报
回复

楼上这个兄弟就写的很好.


febchen() ( ) 信誉:105 2004-12-13 14:29:00 得分: 0


String sd="this , is, a ,test";
String str[];
str=sd.split(",");
System.out.println(str.length);//几个子串组成
System.out.println(str.length-1);//几个","号




Eraserpro 2004-12-14
  • 打赏
  • 举报
回复
String s1 = ",,,,,";
int strs = s1.length()-s1.replaceAll(",","").length();

这个够快了吧,把所有的","都砍了,用原来的长度减一减就出来了
prcgolf 2004-12-14
  • 打赏
  • 举报
回复
up
kaymo 2004-12-13
  • 打赏
  • 举报
回复
那就用indexOf和substring了
suppanda007 2004-12-13
  • 打赏
  • 举报
回复
我给你一个例子,java2编程指南上面的。
public class ExtractSubstring
{
public static void main(String[] args)
{
String text = "i like csdn";
int count = 0;
char separator = ' ';
int index = 0;
do
{
++count;
++index;
index = text.indexOf(separator, index);
}
while (index != -1);


String[] subStr = new String[count];
index = 0;
int endIndex = 0;
for(int i = 0; i < count; i++)
{
endIndex = text.indexOf(separator,index);

if(endIndex == -1)
subStr[i] = text.substring(index);
else
subStr[i] = text.substring(index, endIndex);

index = endIndex + 1;
}

// Display the substrings
for(int i = 0; i < subStr.length; i++)
System.out.println(subStr[i]);
}
}
射天狼 2004-12-13
  • 打赏
  • 举报
回复
用要查找的字符分割字符串,然后求数组的个数就可以了!
buzhiming99 2004-12-13
  • 打赏
  • 举报
回复
是"\\.",去看正则表达式吧
123xxx 2004-12-13
  • 打赏
  • 举报
回复
thomas_20(执子之手,与子偕老) 的方法应该是不错的,
但是Pattern pa=Pattern.compile(",")这句话里面的","换成"."之后竟然是一样的结果(eclipse3.0.1+jdk1.4.2),我不是很明白,请thomas_20兄说明一下Pattern 和Matcher 这两个东西,谢谢
zedk928 2004-12-13
  • 打赏
  • 举报
回复
该说的都说了,我来接分了
buzhiming99 2004-12-13
  • 打赏
  • 举报
回复
不知道楼主是要效率高还是要方便的。

方便的可以这样:

String[] s1 = ",,,,,".ReplaceAll(",",",a").split(",");
s1.length - 1即可
buzhiming99 2004-12-13
  • 打赏
  • 举报
回复
febchen ,试试解析",,,,,"
febchen 2004-12-13
  • 打赏
  • 举报
回复
String sd="this , is, a ,test";
String str[];
str=sd.split(",");
System.out.println(str.length);//几个子串组成
System.out.println(str.length-1);//几个","号

thomas_20 2004-12-13
  • 打赏
  • 举报
回复
Pattern pa=Pattern.compile(",");
String exs = ",,,,";
Matcher ma = pa.matcher(exs);
int i=0;
while(ma.find()){
i++;
}
System.out.println(i);

62,615

社区成员

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

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