java中如何确定一个字符串在另一个字符串中出现的次数

acwy1984 2007-04-09 10:58:01
比如"ok"字符串在"nihaoksdoksad"中出现两次
...全文
34353 43 打赏 收藏 转发到动态 举报
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
uniquePanda 2012-02-14
  • 打赏
  • 举报
回复
public static int getAppearTimes(String target, String appear) {
return (" " + target.toUpperCase() + " ").split(appear.toUpperCase()).length - 1;
}
zhanglingant 2011-10-20
  • 打赏
  • 举报
回复 1
int count = new Regex("ok").Matches("ok.hhh,http://www.5lequ.com ok").Count
yagubai_88 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hacklew1985 的回复:]
String str1 = "nihaoksdoksad ";
char []c=str1.toCharArray();
int total=0;
for(int i=0;i <c.length-1;i++)
if(c[i]== 'o '&&c[i+1]== 'k ')
total++;
System.out.println(str1+ ……
[/Quote]
这还不错,能想到这个
Hi伱好 2011-05-05
  • 打赏
  • 举报
回复
int nQCount = StringUtils.countMatches(sPrepareSql, "?"); //占位符个数
阿门
NPPPNHHH 2011-03-17
  • 打赏
  • 举报
回复
如果不区分大小写呢 求真相
summer200807 2010-06-29
  • 打赏
  • 举报
回复
对我有用,真好
x03570227 2007-08-13
  • 打赏
  • 举报
回复
【同意】 superdullwolf(超级大笨狼,每天要自强,MVP) ( ) 信誉:99 Blog 加为好友 2007-4-9 20:06:20 得分: 0
replace后
长度相减
再除长度
【顶】

public int count(String one, String two) {
int oneLen = one.length();
int twoLen = two.length();
twoLen = twoLen - two.replace(one, "").length();
return twoLen / oneLen;
}

不知道效率怎么样,不过这种方法很有创意,顶一个
simon_vip 2007-05-21
  • 打赏
  • 举报
回复
好贴!!!
octal 2007-04-11
  • 打赏
  • 举报
回复
改掉
public int count(String one, String two) {
int oneLen = one.length();
int twoLen = two.length();
twoLen = twoLen - two.replace(one, "").length();
return oneLen == 0 ? 0 : (twoLen / oneLen);
}
terryhuang 2007-04-11
  • 打赏
  • 举报
回复
同意ls的
("abcabcabc"+"*").split("abc").length - 1
rainv 2007-04-10
  • 打赏
  • 举报
回复
算法有好多^-^
hy_huyang 2007-04-10
  • 打赏
  • 举报
回复
using System.Text.RegularExpressions

private int ReturnCount(string sour, string pattern)
{
Int32 count = 0;
Regex reg = new Regex(pattern);
foreach (Match mc in reg.Matches(sour))
{
count++;
}
return count;
}
rainv 2007-04-10
  • 打赏
  • 举报
回复
先这样写吧,好象还有另一种方法的.
这样应该会快点。^-^

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class TE {
public static void main(String[]args){
TE t=new TE();
String ok="ok";
String ok2="nihao0ksdoksadokok";
System.out.println(" the num is "+t.matchNum(ok, ok2));
ok="ao";
System.out.println(" the num is "+t.matchNum(ok, ok2));
}


/**
*
* @param first 需要匹配的ok
* @param second 被匹配的String
* @return 匹配次数
*/
public int matchNum(String ok,String second){

String pattern="("+ok+"){1}";
System.out.println(" the pattern is "+pattern);
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(second);
int i=0;
while(m.find()){
i++;
}
return i;
}
}
octal 2007-04-10
  • 打赏
  • 举报
回复
【同意】 superdullwolf(超级大笨狼,每天要自强,MVP) ( ) 信誉:99 Blog 加为好友 2007-4-9 20:06:20 得分: 0
replace后
长度相减
再除长度
【顶】

public int count(String one, String two) {
int oneLen = one.length();
int twoLen = two.length();
twoLen = twoLen - two.replace(one, "").length();
return twoLen / oneLen;
}
chrisky2006 2007-04-10
  • 打赏
  • 举报
回复
刚学java那会写的,从屏幕输入两个字符串str1,str2. 判断str1是否包含 str2 希望能对你有所启发。

import java.util.*;
import javax.swing.*;

public class Is_Contain {
public static void main(String[] args)
{

String s1; // The original string
String s2; // compare to s1,to judge whether s1 is contain s2

s1=JOptionPane.showInputDialog("Enter M String:");
s2=JOptionPane.showInputDialog("Enter child String:");
System.out.println("s1:"+s1+"\n"+"s2:"+s2);
char d[];
char e[];


d=s1.toCharArray();
e=s2.toCharArray();
boolean flag=true;


if(s1.length()<s2.length()) //if length of s2 is longer than s1. Return false
{
flag=false;
}
else
{
for(int i=0;i<s1.length();i++)
{
if(e[0]==d[i])
{
for(int j=0;j<s2.length();j++)
{
if(flag==false)
{ flag=true;
}

if(e[j]!=d[i+j])
{ flag=false;break;}
}
}
}
}
}
}
家有萌宝V 2007-04-09
  • 打赏
  • 举报
回复
用数组实现了,不要见笑!
家有萌宝V 2007-04-09
  • 打赏
  • 举报
回复


String str1 = "nihaoksdoksad";
char []c=str1.toCharArray();
int total=0;
for(int i=0;i<c.length-1;i++)
if(c[i]=='o'&&c[i+1]=='k')
total++;
System.out.println(str1+"中含有"+total+"个ok");
daniel_kaka 2007-04-09
  • 打赏
  • 举报
回复
大学教材《数据结构》上,有一章专门讲字串查询和匹配的,并且给出了一个很好的方法,lz有时间可以看看~
约翰羊 2007-04-09
  • 打赏
  • 举报
回复
还是得用indexOf和循环.
顺便同意楼上.
daniel_kaka 2007-04-09
  • 打赏
  • 举报
回复
ls狠~把str换成"abc",但怎么能返回string[],你是不是想用str.split("abc")~
但明显这个方法不行,你可以测一下str="abcabcabc",看看你得到的长度将驶多少~~
加载更多回复(22)

62,623

社区成员

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

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