StringUtils在分割包含字符串的逗号时出现问题

时间:2016-09-08 10:07:09

标签: java arrays apache-stringutils

我想解析这个字符串内容。

   requiredContent='Tigers that breed with lions give birth to hybrids known as "tigons" and "ligers."// In 19th-century Sweden, 380 kids were strangled by their mothers or nurses every year, according to the Swedish Statistical Bureau.'

我用

分开了
 String[] factsArray = StringUtils.split(requiredContent, "//");

我得到了结果

  [Tigers that breed with lions give birth to hybrids known as "tigons" and "ligers.", In 19th-century Sweden,  380 kids were strangled by their mothers or nurses every year,  according to the Swedish Statistical Bureau.]

结果factsArray的数组长度应为2,但它显示的是一个长度为4的数组。它正在解析具有","的字符串。包含在字符串中。这不应该发生,如何修复这个???

3 个答案:

答案 0 :(得分:0)

这只是数组逗号分隔符和字符串逗号

之间显示的冲突

验证 "condition": { "compare": { "ctx.payload.hits.total": { "gt": 0 } } } 结果,它是2而不是4

答案 1 :(得分:0)

我修改了字符串以使用双引号并将它们转义为单词tigons和ligers并试图重现问题。使用ApacheCommons lang3版本3.4:

String requiredContent =  "Tigers that breed with lions give birth to hybrids known as \"tigons\" and \"ligers.\"// In 19th-century Sweden, 380 kids were strangled by their mothers or nurses every year, according to the Swedish Statistical Bureau.";
String[] factsArray = StringUtils.split(requiredContent, "//");

我的数组长度为2.我认为数组内容的显示可能会混乱。

答案 2 :(得分:0)

我的数组长度为2

String requiredContent =  "Tigers that breed with lions give birth to hybrids known as \"tigons\" and \"ligers.\"// In 19th-century Sweden, 380 kids were strangled by their mothers or nurses every year, according to the Swedish Statistical Bureau.";
        List factsArray = StringUtils.split(requiredContent, "//", true);
        for (int i = 0; i < factsArray.size(); i++) {
            System.out.println(factsArray.get(i));
        }
output:

Tigers that breed with lions give birth to hybrids known as "tigons" and "ligers."
 In 19th-century Sweden, 380 kids were strangled by their mothers or nurses every year, according to the Swedish Statistical Bureau.