0

I have a string as follows and I'm splitting the parameters (TransactionType, ServiceID, PaymentID, OrderNumber, Amount) as below.

String response = "TransactionType=SALE&ServiceID=TV3&PaymentID=PYID2016061501417701&OrderNumber=2016061501417701&Amount=235.00"

String splitParams[] = res.split("&");
        String TransactionType = splitParams[0].substring(splitParams[0]
                .indexOf("=") + 1);
        String ServiceID = splitParams[1].substring(splitParams[1]
                .indexOf("=") + 1);
        String PaymentID = splitParams[2].substring(splitParams[2]
                .indexOf("=") + 1);
        String OrderNumber = splitParams[3].substring(splitParams[3]
                .indexOf("=") + 1);
        String amount = splitParams[4].substring(splitParams[4]
                .indexOf("=") + 1);

But the problem is these parameters are not sent in order and sometimes some parameters are not sent. Is there any way to split the parameters in string using java based on parameter name?

2
  • 2
    Why don't you create a Map instead? Commented Jun 15, 2016 at 11:10
  • As I understand, you have an URL fragment. I think you could find plenty of answers here: stackoverflow.com/questions/13592236/… , most of them deal with url, but it should fit also to only parameter part. Commented Jun 15, 2016 at 11:11

2 Answers 2

4

You could use guavas Splitter like this:

Map<String, String> responseMap = Splitter.on("&")
        .omitEmptyStrings()
        .trimResults()
        .withKeyValueSeparator("=")
        .split(response);

And then you could get the values like that:

String TransactionType = responseMp.get("TransactionType ");
Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting error "The method withKeyValueSeparator(String) is undefined for the type Splitter". I have added "com.google.guava_1.6.0.jar". Can u please help with that?
@dmaprasad this function is only from Google Guava, if you want to use it you need to import it like import com.google.common.base.Splitter;
I have imported like com.google.common.base.Splitter;. But same result. not working. do u have any idea. Really appreciate help
I use guava-18.0.jar. The maven-dependency is <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId><version>18.0</version> </dependency>
Thank you very much. By replacing guava-18.0.jar with com.google.guava_1.6.0.jar is worked. Really appreciate your help.
2

Try this:

String response = "TransactionType=SALE&ServiceID=TV3&PaymentID=PYID2016061501417701&OrderNumber=2016061501417701&Amount=235.00";

Map<String,String> responseMap = Arrays.asList(response.split("&")).stream().
     map(v-> v.split("=")).collect(Collectors.toMap(a-> a[0],a ->a[1]));



System.out.println(responseMap);

output:

{TransactionType=SALE, Amount=235.00, PaymentID=PYID2016061501417701, OrderNumber=2016061501417701, ServiceID=TV3}

1 Comment

Thanks for the comment. Above answer really matched with my requirement.

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.