1

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

   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。它正在解析字符串中包含“,”的字符串。这不应该发生,如何解决这个问题???

4

3 回答 3

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。我认为数组内容的显示可能会混乱。

于 2016-09-08T10:15:38.827 回答
0

只是数组逗号分隔符和字符串逗号显示冲突

验证factsArray.length结果,它是 2 而不是 4

于 2016-09-08T10:13:51.003 回答
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.
于 2016-09-08T10:21:50.417 回答