0

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?

Give me any suggestion on this.

Input:

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

Output:

Number value is 34678 
Type values is Internal 
Qty value is 34
2
  • You could use a regex to parse the string. Commented Sep 24, 2019 at 16:33
  • I don't have any idea about regex. Do you give any example for regex? Commented Sep 24, 2019 at 16:38

5 Answers 5

0

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));
      }
Sign up to request clarification or add additional context in comments.

Comments

0

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).

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*)"

Here's a nice explanation of how this works

Comments

0

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"));

Comments

0

I suppose the string is always formatted like that. I.e., n attribute names each followed by a value that does not contain spaces. In other words, the 2n entities are separated from each other by 1 or more spaces.

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);
}

2 Comments

Thanks your code is helpful. But I am facing some errors arrayindexofbound. My input is, String mydata = "Number: 332W3314-64 Type: Internal Qty: 3 Exact
Updated the answer so it considers only the actual pairs of name/value
0

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
Number value is 34678
Type value is Internal
Qty value is 34

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.