2014-11-21 66 views
-2

我有一个webview从服务器中挑选一些html。在Webview中,有两个整数通过%“标记”传递。我使用integer.toString使它可以看作完美工作的文本。现在,在我的HTML中,我有两次%的标记,第一个意思是成为oldPrice,第二个是成为newPrice。webview的拆分字符串Android

然而,两者都成为oldPrice,因为它完全相同。有没有办法在Android(Java)中说,第一个%必须变成oldPrice,第二个%必须变成newPrice?

有人能帮助我走上正确的道路吗?

由于提前, 扎卡里亚

的HTML: Tegen %d punten %@ %.2f欧元VOOR %.2f欧元。

Java代码:

String taglineText = mStrings.getString(ApplicationStrings.ConfigNames.Redeem.REWARD_POINTS_WITH_PRICE).replace("%d", Integer.toString(item.shizzleP)).replace("%@", item.tagline).replace("%.2f",Integer.toString(item.oldPrice)).replace("%.2f", Integer.toString(item.newPrice)); 

PS:我讲的第三%和第四%(%.2f)

+0

这很奇怪。它看起来像https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax格式字符串,但%@不是模式。我会用%s替换%@,然后使用String.format。 – njzk2 2014-11-21 14:05:06

+0

作为进一步的参数,'%.2f'表示一个2位小数的数字,用一个整数替换。这可能不是预期的结果。 – njzk2 2014-11-21 14:06:04

回答

1

好吧,这里是一个简单的解决办法:

String taglineText = mStrings.getString(ApplicationStrings.ConfigNames.Redeem.REWARD_POINTS_WITH_PRICE).replace("%d", Integer.toString(item.shizzleP)).replace("%@", item.tagline).replaceFirst("%\\.2f",Integer.toString(item.oldPrice)).replaceFirst("%\\.2f", Integer.toString(item.newPrice)); 
+0

非常感谢!你能解释我的工作吗? – Zakdroid 2014-11-21 14:04:45

+0

yes dude,replaceFirst方法替换第一个参数的第一个出现,但它以字符串的形式采用正则表达式,这就是为什么我使用“%\\。2f”而不是“%.2f”,现在自第一个模式已被替换,我们再次调用该方法,并将第二种模式视为第一种模式,我们可以得到所需的输出结果,我清楚了吗? – 2014-11-21 14:09:02

+0

是的,我明白了,谢谢你的解释队友:) – Zakdroid 2014-11-21 14:24:58

1

正如评论中所说,你的字符串是format string,除了@,其中肩d可能是s。我会这样做:

String taglineText = String.format(
    mStrings.getString(KEY).replace("%@", "%s"), 
    item.shizzleP, item.tagline, item.oldPrice, item.newPrice);