【问题标题】:Split string of a webview Android拆分webview Android的字符串
【发布时间】:2014-11-21 13:44:12
【问题描述】:

我有一个从服务器中挑选一些 html 的 webview。在 Webview 中,有两个整数由 %“令牌”传递。我使用 integer.toString 使其显示为完美运行的文本。现在在我的 HTML 中,我有两个 % 标记,第一个是 oldPrice,第二个是 newPrice。

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

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

提前致谢, 扎卡里亚

HTML: 泰根 %d 平底锅 %@ %.2f 欧元 %.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));

P.S : 我说的是第三个 % 和第四个 % (%.2f)

【问题讨论】:

  • 这很奇怪。它看起来像 docs.oracle.com/javase/7/docs/api/java/util/… 格式字符串,但 %@ 不是模式。我会用 %s 替换那个 %@,然后使用 String.format。
  • 作为进一步的参数,%.2f 表示具有 2 个小数的数字,您可以将其替换为整数。这可能不是预期的结果。

标签: android webview


【解决方案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));

【讨论】:

  • 工作得很好,谢谢!!你能解释一下它的工作原理吗?
  • 是的,伙计,replaceFirst 方法替换了第一个参数的第一次出现,但它采用字符串形式的正则表达式,这就是我使用“%\\.2f”而不是“%.2f”的原因",现在由于第一个模式已被替换,我们再次调用该方法并替换第二个模式,将其视为第一个模式,我们得到所需的输出,我清楚了吗?
【解决方案2】:

正如 cmets 中所说,您的字符串是 format string,除了 @,它可能应该是 s。我会这样做:

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

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多