解决方案 »
- 大家看看这个异常什么原因引起的Communications link failure due to underlying exception
- 系统集成问题
- java 反编译spring-hibernate-struts源文件时,try、catch代码段中有问题,用什么反编译工具可以解决
- 求助,spring获取数据问题。
- ajax+struts页面跳转错误(急)
- 那位大哥知道在JAVAGUI编程时怎样去掉程序框架栏的JAVA的咖啡碗图标
- 请问哪里有得下载ias等控件啊?
- (满分问题,如果答对的话)求一本书
- Lliux中文字转图片变成框框
- Spring IOC 和DI 的优势在哪?
- Jquery局部刷新问题
- 解析自定义注解问题
{
int count=0;
Reader rd = new FileReader(filename);
int c;
while((c=rd.read())!=-1)
{
while(c==str.charAt(0))//我感觉就是这里错了,但是不知道这里应该怎么判断
{
for (int i = 0; i < str.length(); i++) {
c=rd.read();
if(c!=str.charAt(i))
{
break;
}
if(i==str.length()-1)
{
count++;
}
}
}
}
System.out.println("count="+count);
}
但是不对,显示不出来效果!
public int occurTimes(String s, File f) {
int times=0,index=0;
try {
FileInputStream fis = new FileInputStream(f);
byte[] bs = new byte[(int)f.length()];
fis.read(bs);
String sfile = new String(bs); // 根据文件字符编码,这里可能需要相应调整
while ((index=sfile.indexOf(s,index)) >= 0) {
index += s.length();
times++;
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
return times;
}
public int occurTimes(String s, String bigs) {
int times=0, index=0;
while ((index=bigs.indexOf(s, index)) >= 0) {
index += s.length();
times++;
}
return times;
}
/**
* <p>Counts how many times the substring appears in the larger String.</p>
*
* <p>A <code>null</code> or empty ("") String input returns <code>0</code>.</p>
*
* <pre>
* StringUtils.countMatches(null, *) = 0
* StringUtils.countMatches("", *) = 0
* StringUtils.countMatches("abba", null) = 0
* StringUtils.countMatches("abba", "") = 0
* StringUtils.countMatches("abba", "a") = 2
* StringUtils.countMatches("abba", "ab") = 1
* StringUtils.countMatches("abba", "xxx") = 0
* </pre>
*
* @param str the String to check, may be null
* @param sub the substring to count, may be null
* @return the number of occurrences, 0 if either String is <code>null</code>
*/
public static int countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
count++;
idx += sub.length();
}
return count;
}
org.apache.commons.lang.StringUtils
把while改成if
这是最原始的比较方法,现判断第一个字符是否相同,
然后for (int i = 0; i < str.length(); i++) { //安长度循环判断依次判断后面的字符是否相同
这里的循环有点问题,应该是i=1位置开始,因为i=0在之前说的先比较第一个字符已经判断过了
String sub = "abc";
String[] tests = new String[] {
"abcabc",
"1234abcaabc1234",
"1234abcaabc1234ab1234abcaabc1234",
"1234abcaabc1234ab1234abcaabc1234a"
};
for (String test : tests) {
BufferedReader reader = new BufferedReader(new StringReader(test));
System.out.print(count(reader, sub));
System.out.println(StringUtils.countMatches(test, sub));
reader.close();
}
} // Must be BufferedReader, because the method is not supported in FileReader.
static int count(BufferedReader reader, String substring) throws Exception {
if (substring == null || substring.length() == 0) {
return 0;
}
char[] chars = substring.toCharArray();
char[] buff = new char[substring.length()];
char firstChar = substring.charAt(0);
buff[0] = firstChar;
int count = 0;
int read;
while ((read = reader.read()) != -1) {
char c = (char) read;
if (c == firstChar) {
reader.(substring.length());
int len = reader.read(buff, 1, buff.length - 1);
if (len != buff.length - 1) {
return count;
}
if (Arrays.equals(chars, buff)) {
count++;
} else {
reader.reset();
}
}
}
return count;
}
StringUtils是这个包下面的吗?com.sun.xml.internal.ws.util.StringUtils;