1

I have a string in the following format

Duplicate application\Your request has been rejected by the Credit Bureau server.\Entered value is lower than the minimum requirement to apply with this Income proof document. Please try using any other Income Support Document.\Validation error. Policy criteria not met.\Decisioning System unavailable at the moment\Decision Center error:\We regret not being able to take your application forward at this point. Thank you for applying.

Now, I'm trying to split the string using the delimiter "\". I'm trying to fetch all the strings and compare the string which I receive from the response with the split result each value. I'm not getting the exact thing.. Here is my code..

//Note SCBCC_NEW is the string which I will have..

String[] scbCCNewArray = SCBCC_NEW.split("/");
    for(String results : scbCCNewArray) {
        LOG.info("Value :"+results)
    }

Is it the right way?

1
  • 1
    Yeah that's the right way. Typo correction: .split("\") That's a backslash. Commented Mar 29, 2015 at 6:41

2 Answers 2

4

You will need to escape the back slash as its a special character in java.

String str = "Duplicate application\\Your request has been rejected by the Credit Bureau server.\\Entere";
String[] scbCCNewArray = str.split("\\\\");
for (String results : scbCCNewArray) {
     System.out.println("Value :" + results);
}
Output:
Value :Duplicate application
Value :Your request has been rejected by the Credit Bureau server.
Value :Entere
Sign up to request clarification or add additional context in comments.

Comments

2

An addition to @SMA answer in case you wonder about why do you need four \:

\ is a special character in regex which is used for escaping other special characters. So we need to escape an escape character on a regex level with \\ so that it is treated by regex as a usual character.

Then it comes to java where \ is again a special escape character. So we have to add an escape backslash for each backslash we already have on regex level. That will be 2x2 = 4 total number of backslashes needed.

2 Comments

Thanks Sergey. What's your alternate solution for that?
@Nizam I don't have any solution in mind that would beat String.split()

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.