1

I want to parse this String Content.

   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.'

I split it with

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

and I got the result

  [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.]

The resultant factsArray should have an array length of 2 but it was showing an array of length 4.It was parsing the string which has "," included in the string.This should not happen ,How to fix this ???

uttam
  • 435
  • 9
  • 24

3 Answers3

0

It's just a conflict in the display between array comma separator and string comma

verify factsArray.length result, it's 2 and not 4

andolsi zied
  • 3,553
  • 2
  • 32
  • 43
0

I have modified the string to use double quotes and escape them for the words tigons and ligers and tried to reproduce the problem. Using ApacheCommons lang3 version 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, "//");

And I do get an array length of 2. I think the display of the array content could be messed up.

0

i got array length 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.
bhanu avinash
  • 484
  • 2
  • 16