简体   繁体   English

分割字符串并使用Java获取每个值

[英]Split string and get each values using java

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? 这是正确的方法吗?

You will need to escape the back slash as its a special character in java. 您需要转义反斜杠,因为反斜杠是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

An addition to @SMA answer in case you wonder about why do you need four \\ : 如果您想知道为什么需要四个\\则可以添加@SMA答案

\\ 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 . 然后是Java,其中\\ 再次是特殊的转义字符 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. 这将是2x2 = 4所需的反斜杠总数。

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

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