从字符串中提取值

时间:2021-08-05 15:15:31

I am trying to extract a value from a string.

我试图从字符串中提取一个值。

The string: "at Amazon *APPSTORE.FX rates and charges are applicable.";

字符串:“在亚马逊* APPSTORE.FX费率和费用适用。”;

from the above text I would like extract only: Amazon *APPSTORE

从上面的文字我想只提取:亚马逊* APPSTORE

So I tried the following:

所以我尝试了以下方法:

Log.e("transaction","value "+gettext(str));

public static String gettext(String stringtext)
{
        return stringtext.substring(stringtext.lastIndexOf("at")+2, stringtext.lastIndexOf(".")).trim() ;
}

But I am getting results like this:

但我得到的结果如下:

es and charges are applicable

It is taking at from rates and printing the other string but not the string from first "at"?

它是从速率和打印其他字符串而不是从第一个“at”打印的字符串?

Why?

2 个解决方案

#1


use indexOf() instead of lastIndexOf() for more details check http://www.java-samples.com/showtutorial.php?tutorialid=225

使用indexOf()而不是lastIndexOf()获取更多详细信息,请查看http://www.java-samples.com/showtutorial.php?tutorialid=225

stringtext.substring(stringtext.indexOf("at")+2, stringtext.indexOf(".")).trim())

#2


lastIndexOf() looks for the last occurrence of a substring in an string. So it finds "at" in "rates". Same goes for the "."

lastIndexOf()查找字符串中最后一次出现的子字符串。所以它在“费率”中找到“at”。同样适用于“。”

See http://www.java-samples.com/showtutorial.php?tutorialid=225

For your example you could simply use indexOf() instead. Or even something more specific like:

对于您的示例,您可以简单地使用indexOf()。甚至更具体的东西:

return stringtext.substring(stringtext.indexOf("at Amazon")+2, stringtext.indexOf(".FX")).trim() ;

#1


use indexOf() instead of lastIndexOf() for more details check http://www.java-samples.com/showtutorial.php?tutorialid=225

使用indexOf()而不是lastIndexOf()获取更多详细信息,请查看http://www.java-samples.com/showtutorial.php?tutorialid=225

stringtext.substring(stringtext.indexOf("at")+2, stringtext.indexOf(".")).trim())

#2


lastIndexOf() looks for the last occurrence of a substring in an string. So it finds "at" in "rates". Same goes for the "."

lastIndexOf()查找字符串中最后一次出现的子字符串。所以它在“费率”中找到“at”。同样适用于“。”

See http://www.java-samples.com/showtutorial.php?tutorialid=225

For your example you could simply use indexOf() instead. Or even something more specific like:

对于您的示例,您可以简单地使用indexOf()。甚至更具体的东西:

return stringtext.substring(stringtext.indexOf("at Amazon")+2, stringtext.indexOf(".FX")).trim() ;