使用搜索从字符串中检索子字符串
有没有一种快速的方法可以在另一个字符串中搜索字符串?
我有这样的文件:
<br>
Comment EC00:
<br>
The EC00 is different from EC12 next week. The EC00 much wetter in the very end, which is not seen before.
<br>
<br>
<br>
Comment EC12:
<br>
The Ec12 of today is reliable. It starts cold, but temp are rising. From Sunday normal temp and wet, except for a strengthening high from SE in the very end.
<br>
我已经删除了所有
,我将搜索像“Comment EC12:”这样的字符串来检索后面的内容:
The Ec12 of today is reliable. It starts cold, but temp are rising. From Sunday normal temp and wet, except for a strengthening high from SE in the very end.
或者也许它可以最好保留所有
,这样我至少知道在哪里停止阅读这些行。
PS 这些注释可能在文档中多次出现。
编辑: 我认为这个解决方案对于查找事件来说是可以的,至少是一个很好的起点。 这是最后一个版本,它对我来说非常有用,因为我知道 HTML 中的哪些内容是静态的,哪些不是静态的。但是对于那些想做类似事情的人,您可以重写类似的前两个循环作为最后一个的方式(而不是使用 while 的“if” - 沿着文本文件的行向下)
StringTokenizer parser = new StringTokenizer(weatherComments);
String commentLine = "";
String commentWord = "";
while (parser.hasMoreTokens()) {
if (parser.nextToken().equals("Comment")) {
String commentType = parser.nextToken();
if (commentType.equals(forecastZone + ":")) {
parser.nextToken(); //first occured <br>
commentWord = parser.nextToken();
while(!commentWord.equals("<br>")){
commentLine += commentWord + " ";
commentWord = parser.nextToken();
}
commentLine += "\n";
System.out.println(commentLine);
}
}
}
PPS 在下载大量库以使您的代码看起来更小或更容易理解事物之前,请先考虑如何自己解决它
Is there a fast way to search for string in another string?
I have this kind of a file:
<br>
Comment EC00:
<br>
The EC00 is different from EC12 next week. The EC00 much wetter in the very end, which is not seen before.
<br>
<br>
<br>
Comment EC12:
<br>
The Ec12 of today is reliable. It starts cold, but temp are rising. From Sunday normal temp and wet, except for a strengthening high from SE in the very end.
<br>
I have deleted all the <br>
's and I will be searching for a string like "Comment EC12:" to retrieve what comes after:
The Ec12 of today is reliable. It starts cold, but temp are rising. From Sunday normal temp and wet, except for a strengthening high from SE in the very end.
Or maybe it could be a better idea to leave all the <br>
's so that I will know at least where to stop reading the lines..
P.S. These comments might have multiple occurences in the document.
EDIT:
I think that this solution would be ok for finding the occurences, at least a good place to start..
This is the last version, it works for me very good, because I know what in the HTML will be static and what is not.. But for those, who would like to do something simmilar, you can rewrite first two loops in the simmilar way as the last one(instead of 'if' using while - going down the lines of the text file)
StringTokenizer parser = new StringTokenizer(weatherComments);
String commentLine = "";
String commentWord = "";
while (parser.hasMoreTokens()) {
if (parser.nextToken().equals("Comment")) {
String commentType = parser.nextToken();
if (commentType.equals(forecastZone + ":")) {
parser.nextToken(); //first occured <br>
commentWord = parser.nextToken();
while(!commentWord.equals("<br>")){
commentLine += commentWord + " ";
commentWord = parser.nextToken();
}
commentLine += "\n";
System.out.println(commentLine);
}
}
}
P.P.S.
Before downloading a lot of libraries to make your code look smaller or to understand things easier, think first how to solve it yourself
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试简单地使用
indexOf()
:问题是找到文本的结尾。因此,不替换
并在标签上拆分 HTML 可能会很有用:该示例将打印以下内容:
You can try to simply use
indexOf()
:The problem is to find the end of the text. So it may be useful not to replace the
<br>
and split the HTML on the tags:The example will print the following:
首先我会删除空白行和 < br>我会实现像 BNDM 这样的算法来进行搜索,或者更好地使用像 StringSearch 这样的库。来自网站“Java 中的高性能模式匹配算法” http://johannburkard.de/software/stringsearch/< /a>
Firstly i would remove blank lines and < br > and the i would implement an algorithm like BNDM for searching or better use a library like StringSearch. From the site "High-performance pattern matching algorithms in Java" http://johannburkard.de/software/stringsearch/
根据您想要实现的目标,这可能有点矫枉过正,但我建议您使用有限状态自动机字符串搜索。您可以查看 http://en.literateprograms.org/Finite_automaton_string_search_algorithm_%28Java% 的示例29。
Depending on what you want to achieve, this might be an overkill, but I suggest you use finite state automaton string searching. You ca have a look at an example at http://en.literateprograms.org/Finite_automaton_string_search_algorithm_%28Java%29.