0

I have ONE string field which is in format:

"TransactionID=30000001197169 ExecutionStatus=6
        additionalCurrency=KMK
    pin= 0000"

So they are not separated with some ; оr , they are not seperated even with one blank space.

I want to get value for Execution Status and put it in some field? How to achieve this?

Thanks for help

5 Answers 5

3

This works. But I am not sure this is the most optimal.It just solves your problem.

    String s = "TransactionID=30000001197169ExecutionStatus=6additionalCurrency=KMKpin=0000";
    if(s!=null && s.contains("ExecutionStatus="))
    {
        String s1[] = s.split("ExecutionStatus=");
        if(s1!=null &&  s1.length>1)
        {
            String line = s1[1];
            String pattern = "[0-9]+";

              // Create a Pattern object
              Pattern r = Pattern.compile(pattern);

              // Now create matcher object.
              Matcher m = r.matcher(line);
              if (m.find( )) {
                 System.out.println("Match");
                 System.out.println("Found value: " + m.group(0) );

              } else {
                 System.out.println("NO MATCH");
              }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

The above code is dynamic. It does not matter what comes after 6 or what comes before ExecutionStatus= . It will only search for the value of ExecutionStatus as you desired
As you can see the code has used regular expression. it searches [0-9+] means any length of numeric value.
2

In your example they are indeed seperated by blanks, but the following should be working without blanks, too. Assuming your String is stored in String arguments

String executionStatus;
String[] anArray = arguments.split("=");
for (int i; i < anArray.length; i++)
    if (anArray[i].contains("ExecutionStatus")){
        executionStatus = anArray[++i].replace("additionalCurrency","");
        executionStatus = executionStatus.trim();
    }
}

3 Comments

my result is "6 additionalCurrency" so I must cut additionalCurrency from result
yes thanks. one small question - what if additionalCurrency is not always the second parameter in string? Thanks for help
first, it doesn't matter, which place executionStatus is. You should trim the string correctly, when the order can change. either you replace all occurences of parameters like "additionalCurrency" with "", or you trim the string with other options (possibily a blank yet).
2

Check if it contains() ExecutionStatus=

If yes then split the string with ExecutionStatus=

Now take the Second string from array find the first occurance of non digit char and use substring()

2 Comments

can you write me the shortest cod for this? sorry for disturbing
I have given enough(dirty) information, Please check api doc and try to write by yourself
1

Assuming all that white space is present in your string, this works.

String str = "\"TransactionID=30000001197169 ExecutionStatus=6\n" +
        "        additionalCurrency=\"KMK\"\n" +
        "    pin= \"0000\"\"";
int start = str.indexOf("ExecutionStatus=") + "ExecutionStatus=".length();
int status = 0;

if (start >= 0) {
    String strStatus = str.substring(start, str.indexOf("additionalCurrency=") - 1);
    try {
        status = Integer.parseInt(strStatus.trim());
    } catch (NumberFormatException e) {
    }
}

Comments

1

At the risk of attracting "... and now you have two problems!" comments, this is probably easiest done with regexes (str is the String defined above):

Pattern p = Pattern.compile("ExecutionStatus\\s*=\\s*(\\d+)"); // Whitespace matching around equals for safety, capturing group around the digits of the status)
Matcher m = p.matcher(str);
String status = m.find() ? m.group(1) : null;

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.