【问题标题】:Extract numeric values between 2 characters in EditText android在EditText android中提取2个字符之间的数值
【发布时间】:2017-05-26 09:10:44
【问题描述】:

我在 EditText 中显示了食品价格列表,那么如何遍历它们并提取所有价格?例如,在这里我想提取 : 和 L 之间的价格。

我正在尝试使用 StringTokenizer 但不知道如何使用它...

【问题讨论】:

  • 使用单个正则表达式去除除数字之外的所有数字。
  • 你能给我代码吗
  • 你可以很容易地用谷歌搜索。

标签: android stringtokenizer


【解决方案1】:

试试这个

StringTokenizer tokenizer = new StringTokenizer(string, ":L");
while (tokenizer.hasMoreTokens()) {
    try {
        int price = Integer.parseInt(tokenizer.nextToken());
    } catch (NumberFormatException e) {
        continue;
    }
}

【讨论】:

    【解决方案2】:

    使用正则表达式尝试以下操作:

    final String s = "Price:1500L.L";
    final Pattern p = Pattern.compile(":.*?L");
    final Matcher m = p.matcher(s);
    if (m.find()) {
        final String result = m.group().subSequence(1, m.group().length() - 1).toString();
        //result = 1500
    }
    

    【讨论】:

      【解决方案3】:

      我用 Java 测试了一些代码。这可能会有所帮助

      private static int getSum(String test) {
          String[] res1 = test.replace("Price:", "-").replace("L.L", "-").split("-");
          int sum = 0;
          for (String s : res1) {
              try {
                  sum += Integer.valueOf(s);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          return sum;
      }
      

      【讨论】:

      • 谢谢...希望你能提出我的问题+1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 2016-08-20
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      相关资源
      最近更新 更多