【问题标题】:I need to get a substring from a java string Tokenizer我需要从 Java 字符串 Tokenizer 中获取子字符串
【发布时间】:2013-06-15 06:16:42
【问题描述】:

我需要从 java 字符串标记器中获取子字符串。

我的输入字符串是 = Pizza-1*Nutella-20*Chicken-65*

        StringTokenizer productsTokenizer = new StringTokenizer("Pizza-1*Nutella-20*Chicken-65*", "*");
        do
        {
            try
            {
                int pos = productsTokenizer .nextToken().indexOf("-");
                String product = productsTokenizer .nextToken().substring(0, pos+1);
                String count= productsTokenizer .nextToken().substring(pos, pos+1);
                System.out.println(product + "   " + count);
            }
            catch(Exception e)
            {

            }
        }
        while(productsTokenizer .hasMoreTokens());

我的输出必须是:

Pizza  1
Nutella  20
Chicken  65

我需要将产品值和计数值放在单独的变量中,以便将这些值插入数据库中。

我希望你能帮助我。

【问题讨论】:

  • 为什么不简单地将*- 替换为空格?
  • 你真的不应该使用 StringTokenizer,因为它的 Javdocs 声明。
  • @MarounMaroun 不,因为我需要将产品和计数放在单独的变量中,在我必须将该值插入数据库之后。
  • 不确定您是否熟悉正则表达式,但这对他们来说是一个很好的应用程序。有兴趣可以看看我的回答。

标签: java string netbeans substring tokenize


【解决方案1】:

你可以使用String.split()作为

String[] products = "Pizza-1*Nutella-20*Chicken-65*".split("\\*");

for (String product : products) {
    String[] prodNameCount = product.split("\\-");
    System.out.println(prodNameCount[0] + " " + prodNameCount[1]);
}

输出

Pizza  1
Nutella  20
Chicken  65

【讨论】:

  • @darthlitox 不要忘记将答案标记为正确答案 :)
  • @darthlitox 不客气。如果这解决了您的问题,请接受答案。谢谢。
【解决方案2】:

您调用 nextToken() 方法 3 次。这将为您提供 3 个不同的令牌

int pos = productsTokenizer .nextToken().indexOf("-");
String product = productsTokenizer .nextToken().substring(0, pos+1);
String count= productsTokenizer .nextToken().substring(pos, pos+1);

相反,您应该执行以下操作:

String token = productsTokenizer .nextToken();
int pos = token.indexOf("-");
String product = token.substring(...);
String count= token.substring(...);

我会让你找出 substring() 方法的正确索引。

除了使用 do/while 结构,最好只使用 while 循环:

while(productsTokenizer .hasMoreTokens())
{
    // add your code here
}   

那就是不要假设有一个令牌。

【讨论】:

    【解决方案3】:

    如果您的输入增加,您可能想要使用的替代答案:

    // find all strings that match START or '*' followed by the name (matched),
    // a hyphen and then a positive number (not starting with 0)
    Pattern p = Pattern.compile("(?:^|[*])(\\w+)-([1-9]\\d*)");
    Matcher finder = p.matcher(products);
    while (finder.find()) {
        // possibly check if the new match directly follows the previous one
        String product = finder.group(1);
        int count = Integer.valueOf(finder.group(2));
        System.out.printf("Product: %s , count %d%n", product, count);
    }
    

    【讨论】:

      【解决方案4】:

      有些人不喜欢正则表达式,但这对他们来说是一个很好的应用程序。您只需要使用"(\\w+)-(\\d{1,})\\*" 作为您的模式。这是一个玩具示例:

          String template = "Pizza-1*Nutella-20*Chicken-65*";
          String pattern = "(\\w+)-(\\d+)\\*";
      
          Pattern p = Pattern.compile(pattern);
          Matcher m = p.matcher(template);
      
          while(m.find())
          {
              System.out.println(m.group(1) + " " + m.group(2)); 
          }
      

      为了进一步解释这一点,"(\\w+)-(\\d+)\\*" 查找(\\w+),它是[A-Za-z0-9_] 中至少有1 个字符的任意集合,后跟-,后跟数字\\d+,其中+ 表示长度至少为一个字符,后跟一个*,必须转义。括号捕获其中的内容。此正则表达式中有两组捕获括号,因此我们通过group(1)group(2) 引用它们,如while 循环中所示,它打印:

      Pizza 1
      Nutella 20
      Chicken 65
      

      【讨论】:

        猜你喜欢
        • 2013-06-27
        • 2017-10-16
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多