这一道java面试题是在很早出来的时候,在一家大型的上市公司面试的题,不过当时交了白卷。现在将这道java面试题的答案整理出来。

 

package com.buyli.interview;
/**
* @Copyright @ 2012
*java面试题
* @version 创建时间:Created on 2012-10-17
* @author 作者:Create bywww.360buyli.com
* @Email:360buyli@gmail.com
* @description 如何在指定的内容中找出指定字符串的个数
*
*/
import java.io.*;
import java.util.regex.*;

public class Word
{
// 查找其中字符串”me”单词的数量
private static final String matcherStr = “me”;

public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(
“E:\\content.txt”));
StringBuffer sb = new StringBuffer();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
System.out.println(sb.toString()); //打印出文件的内容
Pattern expression = Pattern.compile(“[a-zA-Z]+”);
Matcher matcher = expression.matcher(sb.toString().toLowerCase());
int i = 0;
while (matcher.find()) {
if (matcher.group().toLowerCase()
.equals(matcherStr.toLowerCase())) {
i++;
}
}
System.out.println(“此单词的数量是:” + i + “个”);
} catch (Exception e) {
System.out.println(e.toString());
}

}
}