這一道java面試題是在很早出來的時候,在一家大型的上市公司面試的題,不過當時交了白卷。如今將這道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」; express
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());
} app
}
} ip