【练习】判断一个字符串中某些字段出现的次数


package com.heima.test;

import java.io.*;

public class wordtext {
public static void main(String[] args) {
String s = " ";
String w = " ";
System.out.print("input:\n");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
s = in.readLine();
System.out.println(s);
} catch (IOException e) {
}
System.out.print("input text word!");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
w = in.readLine();
System.out.println(w);
} catch (IOException e) {
}


int wLen = w.length();
int sLen = s.length();
int beginIndex = 0;
int endIndex = wLen;
int count = 0;
//sLen - wLen就是走过的距离
for(int i = 0; i < sLen - wLen; i++){
String word = s.substring(beginIndex, endIndex);
if(word.equalsIgnoreCase(w)){
count++;
}
beginIndex++;
endIndex++;
}
if (count == 0)
System.out.println("there no " + w);
else
System.out.println("there" + count + w);


// int first = 0, second = 0, end = 0;
// for (int i = 1; i <= s.length(); i += second + 1) {
// second = s.indexOf(" ");
// String t = s.substring(first, second);
// s = s.substring(second + 1);
// if (w.equalsIgnoreCase(t))
// end++;
// }
// if (end == 0)
// System.out.println("there no " + w);
// else
// System.out.println("there" + end + w);
}
}


是别人提出问题代码后,我修改后得出的,有不足,不是自己的就没去改了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值