解析网页后,我找到了这一行
“JANATAMF 7.30 -0.20 -2.67%1STBSRS 80.00 -3.60 -4.31%1STPRIMFMF 12.90 -0.80 -5.84%6THICB 40.60 -2.50 -5.80%8THICB 50.00 -1.30 -2.53%ABB1STMF 9.90 0.30 3.13%ACI 188.00 -5.90 -3.04 %ACIFORMULA 82.70 0.50 0.61%“
现在我想为任何输入找到相应的整数值,例如 如果我输入一个输入JANATAMF,它将单独打印7.30 -0.20 -2.67%
我写了一段代码,但看起来并不好......
public void stringfinder(String linktext,String input) throws NumberFormatException
{
try
{
if(linktext!=null)
{
String finalstr=linktext;
//System.out.print(finalstr);
int inplen=input.length();
if(finalstr.contains(input)) //CHECK THE USER INPUT IS IN STRING OR NOT WHICH RETURN BOOLEAN
{
//System.out.println("found");
int index=finalstr.indexOf(input); //FIND THE INDEX OR STARTING POINT OF THE STRING
//System.out.print(index);
int stpr=index+inplen+1; //AS WE VE TO PRINT THE NEXT STRING FROM THE INPUT STRING
//WE ADD THE STARTING POINT OF THE INPUT STRING AND THE LENGTH
//OF THE INPUT STRING
//int first=0;
int i=0;
String x="" ;
String ary[]=new String[10];
while(finalstr.charAt(stpr)!=' ' ) //PRINT UNTIL FIND A GAP
{
//System.out.print(finalstr.charAt(stpr));
Character c=new Character(finalstr.charAt(stpr));
String s=c.toString(c);
ary[i]=s;
x=x+ary[i];
i++;
stpr++;
//first++;
}
//System.out.print(x);
String yo=x;
System.out.print(yo);
//double d = Double.valueOf(yo.trim()).doubleValue();
/*int secstpr=stpr+1;
int second=0;
while(finalstr.charAt(secstpr)!=' ')
{
System.out.print(finalstr.charAt(secstpr));
secstpr++;
second++;
}*/
//System.out.print(second);
}
}
}
所以请帮助......
答案 0 :(得分:0)
这就是@kev所指的:
public String stringfinder(String linktext, String input) {
String [] inputSplits = input.split("\\s+");
for (int i = 0; i < inputSplits.length; i += 4) {
if (inputSplits[i].equals(linktext) {
return inputSplits[i + 1] + " " + inputSplits[i + 2]
+ " " + inputSplits[i + 3];
}
}
return "";
}
这将返回您要搜索的字符串。我仍然建议你在这里连接字符串:return inputSplits[i + 1] + " " + inputSplits[i + 2] + " " + inputSplits[i + 3];
你更聪明地解释它们(例如构造数字)。