拆分字符串并使用java获取每个值

时间:2015-03-29 06:36:26

标签: java string

我有一个以下格式的字符串

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.

现在,我尝试使用分隔符" \"来分割字符串。我试图获取所有字符串,并将从响应中收到的字符串与每个值的拆分结果进行比较。我没有得到确切的东西..这是我的代码..

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

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

这是正确的方法吗?

2 个答案:

答案 0 :(得分:4)

您需要将反斜杠转义为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

答案 1 :(得分:2)

如果您想知道为什么需要四个\,请添加@SMA answer

\是正则表达式中的一个特殊字符,用于转义其他特殊字符。因此,我们需要使用\\在正则表达式级别上转义转义符,以便将正则表达式视为常用字符。

然后来到java \ is again a special escape character。因此,我们必须为正则表达式级别上已有的每个反斜杠添加一个转义反斜杠。这将是2x2 = 4所需的反斜杠总数。