简体   繁体   English

从 Java 中的字符串中获取特定的数据值(字符串不带逗号)

[英]Get a specific data values from a string in Java (String without comma)

I want to get the value from a string.我想从字符串中获取值。

I have a string value like this:我有一个这样的字符串值:

String myData= "Number: 34678 Type: Internal Qty: 34";

How can I get the Number, Type, Qty values separately?如何分别获取 Number、Type、Qty 值?

Give me any suggestion on this.给我任何建议。

Input:输入:

String myData= "Number: 34678 Type: Internal Qty: 34";

Output: Output:

Number value is 34678 
Type values is Internal 
Qty value is 34

I suppose the string is always formatted like that.我想字符串总是这样格式化的。 Ie, n attribute names each followed by a value that does not contain spaces.即, n属性名称,每个属性名称后跟一个不包含空格的值。 In other words, the 2n entities are separated from each other by 1 or more spaces.换言之,这2n实体彼此相隔 1 个或多个空格。

If so, try this:如果是这样,试试这个:

String[] parts;
int      limit;
int      counter;
String   name;
String   value;
parts = myData.split("[ ]+");
limit = (parts.length / 2) * 2; // Make sure an even number of elements is considered
for (counter = 0; counter < limit; counter += 2)
{
  name = parts[counter].replace(":", "");
  value = parts[counter + 1];
  System.out.println(name + " value is " + value);
}

You can use regex to do this cleanly:您可以使用正则表达式干净地做到这一点:

Pattern p = Pattern.compile("Number: (\\d*) Type: (.*) Qty: (\\d*)");
Matcher m = p.matcher(myData);
m.find()

So you'll get the number with m.group(1) , the Type m.group(2) and the Qty m.group(3) .所以你会得到m.group(1)的数字,类型m.group(2)和数量m.group(3)

I assume you accept a limited number of types.我假设您接受有限数量的类型。 So you can change the regex to match only if the type is correct, for eg.因此,您可以更改正则表达式以仅在类型正确的情况下匹配,例如。 either Internal or External: "Number: (\\d*) Type: (Internal|External) Qty: (\\d*)"内部或外部: "Number: (\\d*) Type: (Internal|External) Qty: (\\d*)"

Here's a nice explanation of how this works这是一个很好的解释这是如何工作的

Here is one way to do it.这是一种方法。 It looks for a word following by a colon followed by zero or more spaces followed by another word.它查找后跟冒号的单词,后跟零个或多个空格,后跟另一个单词。 This works regardless of the order or names of the fields.无论字段的顺序或名称如何,这都有效。

      String myData = "Number: 34678 Type: Internal Qty: 34";
      Matcher m = Pattern.compile("(\\S+):\\s*(\\S+)").matcher(myData);
      while (m.find()) {
         System.out.println(m.group(1) + " value is " + m.group(2));
      }

If you just want to print them with fixed pattern of input data, a simplest way is shown as follows: (Just for fun!)如果您只想使用固定模式的输入数据打印它们,最简单的方法如下所示:(只是为了好玩!)

System.out.print(myData.replace(" Type", "\nType")
            .replace(" Qty", "\nQty")
            .replace(":", " value is"));

This Should work这应该工作

    String replace = val.replace(": ", "|");

    StringBuilder number = new StringBuilder();
    StringBuilder type = new StringBuilder();
    StringBuilder qty = new StringBuilder();
    String[] getValues = replace.split(" ");
    int i=0;
    while(i<getValues.length-1){
        String[] splitNumebr = getValues[i].split("\\|");
        number.append(splitNumebr[1]);
        String[] splitType = getValues[i+=1].split("\\|");
        type.append(splitType[1]);
        String[] splitQty = getValues[i+=1].split("\\|");
        qty.append(splitQty[1]);
    }
    System.out.println(String.format("Number value is %s",number.toString()));
    System.out.println(String.format("Type value is %s",type.toString()));
    System.out.println(String.format("Qty value is %s",qty.toString()));

}

Output Output
Number value is 34678数值为 34678
Type value is Internal类型值为内部
Qty value is 34数量值为 34

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM