比如"ok"字符串在"nihaoksdoksad"中出现两次
解决方案 »
- 三年JavaEE开发积累的那些代码之一:JavaSE篇
- jdk源码中问题
- alt 切换 到 菜单栏的问题
- 问一个关于serverSocket的问题
- JDOM操作高手请进
- 一个大专生痛心的决定,何去何从....前面的路要如何去走....
- 我刚学Java,请问..
- 临近JAVA考试,还有两个关于位运算的菜鸟问题请求各位高手帮忙。
- 请教,在jbuilder中的两个窗口的关闭问题?谢谢
- 请帮忙译一下这句话,谢谢
- short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
- clob.getCharacterOutputStream()方法被替换掉了吗
String str2 = "ok";
int total = 0;
for (int i = 0; i < str1.length(); i ++){
if(str1.indexOf(str2) == 0){
total ++;
}
}
System.out.println(str1+"中含有"+total+"个"+str2);
String str1 = "nihaoksdoksad";
String str2 = "ok";
int total = 0;
for (String tmp = str1; tmp != null&&tmp.length()>=str2.length();){
if(tmp.indexOf(str2) == 0){
total ++;
}
tmp = tmp.substring(1);
}
System.out.println(str1+"中含有"+total+"个"+str2);
上面我写的算法是字符可以覆盖的查找:
下面给你一种不允许字符覆盖的查找:
String str1 = "nihaokokosdokosad";
String str2 = "oko";
int total = 0;
for (String tmp = str1; tmp != null&&tmp.length()>=str2.length();){
if(tmp.indexOf(str2) == 0){
total ++;
tmp = tmp.substring(str2.length());
}else{
tmp = tmp.substring(1);
}
}
System.out.println(str1+"中含有"+total+"个"+str2);对于
String str1 = "nihaokokosdokosad";
String str2 = "oko";
两种查找结果是有区别的,不知道lz要哪种?
但明显这个方法不行,你可以测一下str="abcabcabc",看看你得到的长度将驶多少~~
顺便同意楼上.
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");
你写程序应该有供用性,你能只就事论事,
如果把"ok"改成"ook"或别的什么,那你的程序步就要重新写了~你写的时候就用该考虑,这段程序或这个函数怎么能够适用所有的串,这样才可以称之为比较完整的程序~
int count=0;
while(str.indexOf(token)!=-1){
count++;
str = str.substring(str.indexOf(token)+token.length());
}
System.out.println(token+"出现的次数:"+count);
return count;
}
char []c=str1.toCharArray();
int total=0;
for(int i=0;i<c.length-2;i++)
if(c[i]=='o'&&c[i+1]=='o'&&c[i+2]='k')
total++;
System.out.println(str1+"中含有"+total+"个ok");//你写的时候就用该考虑,这段程序或这个函数怎么能够适用所有的串,这样才可以称之为比较完整的程序~
我觉得你这句话明显就有问题了 ,如果一个程序能解决所有的问题,那还要程序员干什么
String target = "ok";
int count = 0;
while (Pattern.compile(Pattern.quote(target)).matcher(
input).find()) {
count++;
}count就是你想要的!
*计算一个字符串在另一个字符串中出现的次数
*@author : Eastsun
*/
public class Count{
public static int count(String text,String sub){
int count =0, start =0;
while((start=text.indexOf(sub,start))>=0){
start += sub.length();
count ++;
}
return count;
}
public static void main(String[] args){
String text ="nihaoksdoksad";
String sub ="ok";
System.out.println(count(text,sub));
}
}
String str1 = "nihaoksdoksad";
char []c=str1.toCharArray();
int total=0;
for(int i=0;i<c.length-2;i++)
if(c[i]=='o'&&c[i+1]=='o'&&c[i+2]='k')
total++;
System.out.println(str1+"中含有"+total+"个ok");//你写的时候就用该考虑,这段程序或这个函数怎么能够适用所有的串,这样才可以称之为比较完整的程序~
我觉得你这句话明显就有问题了 ,如果一个程序能解决所有的问题,那还要程序员干什么
不要生气,我不是针对个人的~我觉得你应该看一看什么叫做合格的程序,什么叫合格的程序员,也就是想做一个合格的程序员的起码条件~~这些都是在写程序之前有必要知道的~~像这种程序,没人愿意去维护的~
int result = 0;
String strCheck = new String(strTarget);
for (int i = 0; i < strTarget.length();) {
System.out.println(i);
int loc = strCheck.indexOf(strSearch);
if (loc == -1) {
break;
} else {
result++;
i = loc + strSearch.length();
strCheck = strCheck.substring(i);
}
}
return result;
}
public static void main(String[] args){
String str = "aaabbbcccdddaaabbccddaabbcccdd";
String subStr = "aa";
String replaceString = "ff";
int total = 0;
StringBuffer finalStr = new StringBuffer();
int begin = 0;
int end = 0;
while((end = str.indexOf(subStr,begin)) >= 0){
finalStr.append(str.substring(begin,end));
finalStr.append(replaceString);
begin = end + subStr.length();
total++;
}
finalStr.append(str.substring(begin));
System.out.println(finalStr);
System.out.println("total =" + total);
}
}
System.out.println(str.length() - str.replaceAll("ab", "a").length());
长度相减
再除长度
这样应该会快点。^-^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;
}
}
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;}
}
}
}
}
}
}
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;
}
{
Int32 count = 0;
Regex reg = new Regex(pattern);
foreach (Match mc in reg.Matches(sour))
{
count++;
}
return count;
}
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);
}
("abcabcabc"+"*").split("abc").length - 1
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;
}不知道效率怎么样,不过这种方法很有创意,顶一个
阿门
return (" " + target.toUpperCase() + " ").split(appear.toUpperCase()).length - 1;
}