---
title: "如何在指定的内容中找出指定字符串的个数 "
author: "hyh2011"
date: "2012-10-26T15:36:45Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-11-14T00:01:16Z"
id: 1706112
url: "https://hyh2011.iteye.com/blog/1706112"
---

这一道[](http://www.360buyli.com/)[**java**]()面试题是在很早出来的时候，在一家大型的上市公司面试的题，不过当时交了白卷。现在将这道[java面试题](http://www.360buyli.com/)的答案整理出来。

package com.buyli.interview;  
/\**  
\* @Copyright @ 2012  
\*[java面试题](http://www.360buyli.com/)  
\* @version 创建时间：Created on 2012-10-17  
\* @author 作者：Create by[www.360buyli.com](http://www.360buyli.com/)  
\* @Email:[360buyli@gmail.com](mailto: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());  
}

}  
}