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
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: