解析 HTML 时,每次我有一个 '>' char
,我都需要检查它后面是否有一个数字。该号码可以是 1、2 或 3 位数字。
代码看起来不错,但我总是得到一个StringIndexOutOfBoundException
.
代码:
while (matches < 19)
{
more = dataInHtml.indexOf(">",index);
nextOne = dataInHtml.charAt(more + 1);
nextTwo = dataInHtml.charAt(more + 2);
nextThree = dataInHtml.charAt(more + 3);
if (Character.isDigit(nextOne)) digitOne = true;
if (Character.isDigit(nextTwo)) digitTwo = true;
if (Character.isDigit(nextThree)) digitThree = true;
if (digitThree)
{
data[matches] = dataInHtml.substring(more + 1, 3);
matches++;
digitThree = false;
digitTwo = false;
digitOne = false;
index = more + 3;
itWasADigit = true;
}
if (digitTwo)
{
data[matches] = dataInHtml.substring(more + 1, 2);
matches++;
digitTwo = false;
digitOne = false;
index = more + 2;
itWasADigit = true;
}
if (digitOne)
{
data[matches] = dataInHtml.substring(more + 1, 1);
matches++;
digitOne = false;
index = more + 1;
itWasADigit = true;
}
if (!(itWasADigit))
{
index = more + 1;
itWasADigit = false;
}
}