Java “查找所有字符串”;";在.txt文件中
这是我的密码:Java “查找所有字符串”;";在.txt文件中,java,Java,这是我的密码: // Import io so we can use file objects import java.io.*; public class SearchThe { public static void main(String args[]) { try { String stringSearch = "the"; // Open the file c:\test.txt as a buffered rea
// Import io so we can use file objects
import java.io.*;
public class SearchThe {
public static void main(String args[]) {
try {
String stringSearch = "the";
// Open the file c:\test.txt as a buffered reader
BufferedReader bf = new BufferedReader(new FileReader("test.txt"));
// Start a line count and declare a string to hold our current line.
int linecount = 0;
String line;
// Let the user know what we are searching for
System.out.println("Searching for " + stringSearch + " in file...");
// Loop through each line, stashing the line into our line variable.
while (( line = bf.readLine()) != null){
// Increment the count and find the index of the word
linecount++;
int indexfound = line.indexOf(stringSearch);
// If greater than -1, means we found the word
if (indexfound > -1) {
System.out.println("Word was found at position " + indexfound + " on line " + linecount);
}
}
// Close the file after done searching
bf.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
}
我想在test.txt文件中找到一些单词“”。问题是,当我找到第一个“”时,我的程序停止查找更多
当一些单词如“那么”我的程序将其理解为单词“”,您不应该使用indexOf,因为它会找到字符串中所有可能的子字符串。因为“then”包含字符串“the”,所以它也是一个很好的子字符串 索引 public int indexOf(字符串str, int fromIndex)返回此字符串中的索引 第一次发生的 指定的子字符串,从 指定的索引。返回的整数 是最小值k,其中: 你应该把这些行分成许多单词,在每个单词上循环,并与“the”进行比较 上面的代码片段还将为您循环行中所有可能的“The”。使用indexOf将始终返回您最好用于此类搜索的第一个匹配项。 作为一个简单/肮脏的解决方法,您可以从
String stringSearch = "the";
到
您当前的实现将只找到每行的第一个“the”实例 考虑将每行拆分为单词,在单词列表上迭代,并将每个单词与“the”进行比较:
while (( line = bf.readLine()) != null)
{
linecount++;
String[] words = line.split(" ");
for (String word : words)
{
if(word.equals(stringSearch))
System.out.println("Word was found at position " + indexfound + " on line " + linecount);
}
}
不区分大小写地使用正则表达式,以单词边界查找“the”的所有实例和变体
indexOf(“the”)
无法区分“the”和“then”,因为两者都以“the”开头。同样地,在<强>“诅咒”<<强> > 中发现“”。
要避免这种情况,请使用正则表达式,并搜索“the”,两边都有单词边界(\b
)。使用单词边界,而不是在“”上拆分,或者只使用索引(“the”)
(两边的空格),这样就不会在标点旁边找到“the.”和其他实例。您也可以不敏感地进行搜索,以查找“”中的“”
Pattern p = Pattern.compile("\\bthe\\b", Pattern.CASE_INSENSITIVE);
while ( (line = bf.readLine()) != null) {
linecount++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while (m.find()) {
System.out.println("Word was found at position " +
m.start() + " on line " + linecount);
}
}
听起来,这个练习的目的并不是要让你熟练掌握正则表达式(我不知道这可能是……但这似乎有点基本),尽管regexs确实是解决这类问题的现实解决方案
我的建议是关注基本内容,使用index of和substring来测试字符串。考虑如何解释字符串的自然大小写敏感特性。另外,您的阅读器是否总是关闭(即是否有办法不执行bf.close())您是否考虑过使用Java的正则表达式包(Java.util.regex)?您可以在这里找到一些有用的示例。这不是答案。这是一种批评。首先,我只是试图找出他存在的问题,而索引方法就是问题所在。然后,我找到另一个好方法去做他想做的事。有什么不对劲吗?是的,你在骗我。发帖前写一个完整的答案。在
上拆分“
将找不到“the.”或“the”的实例。你可以做很多equals(…)
和indexOf(…)
测试,正则表达式在这方面要灵活得多。啊,是的,对于这样的特殊情况是可能的。对于商业,我认为regrex应该是最好的,但我不确定这篇作业是否要求在行尾或行首使用muchDoesn。如果“the”在行首、行尾、特殊字符或大写字母之前,这将不起作用。regex使用+1,比其他“split”选项好得多(包括我的)。
while (( line = bf.readLine()) != null)
{
linecount++;
String[] words = line.split(" ");
for (String word : words)
{
if(word.equals(stringSearch))
System.out.println("Word was found at position " + indexfound + " on line " + linecount);
}
}
Pattern p = Pattern.compile("\\bthe\\b", Pattern.CASE_INSENSITIVE);
while ( (line = bf.readLine()) != null) {
linecount++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while (m.find()) {
System.out.println("Word was found at position " +
m.start() + " on line " + linecount);
}
}