I'm trying to pull out sub-string from a string using java.Util.Scan
The sub-string is between " <TD class=MoreB align=center>
" and " </TD>
" in the original string
This is the code:
public static String pullStringOut(String str)
{
String stringer = null;
Scanner scanner = new Scanner(str);
scanner.findInLine("<TD class=MoreB align=center>");
while (scanner.hasNext() && scanner.next() != "</TD>")
{
stringer+= " " + (scanner.next());
}
return stringer;
}
but it's not working well.
From the original string:
" <TD class=MoreB align=center>TextTextTextText</TD></TR></TABLE> }
"
I get the following result:
"
TextTextTextText</TD></TR></TABLE> }
"
Instead of the expected
"TextTextTextText"
A few problems:
scanner.next() != "</TD>"
will always be true
as the operands will not be the same object. Use !scanner.next().equals("</TD>")
. From Reference Equality Operators == and != section of the JLS :
The result of != is false if the operand values are both null or both refer to the same object or array; otherwise, the result is true.
scanner.next()
is being called twice on each iteration of the loop. Change to:
String line; while (scanner.hasNext() && !(line = scanner.next()).equals("</TD>")) { stringer+= " " + line; }
You can use a Regex Expresssion.
Something like :
Pattern p = Pattern.compile("/\<TD class=MoreB align=center>(.*)\<\/td\>/");
Matcher m = p.matcher(str);
while(m.find()) {
//do whatever you want here
}
(not tested)
Here is an alternate solution:
String tvt ="<TD class=MoreB align=center>TextTextTextText</TD></TR></TABLE> }" //your original string
String s ="<TD class=MoreB align=center>";
String f= "</TD>";
int sindex =tvt.indexOf(s);
int findex =tvt.indexOf(f);
String fs = "";
if(sindex!=-1 && findex!=-1)
fs=tvt.substring(sindex+s.length(), findex); // your desired substring
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.