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

結果の factArray の配列の長さは 2 である必要がありますが、長さ 4 の配列を示していました。文字列に含まれる「,」を含む文字列を解析していました。これは発生しないはずです。これを修正するには???

4

3 に答える 3

0

配列のコンマ区切りと文字列のコンマの間の表示の競合です

結果を確認factsArray.lengthしてください。4 ではなく 2 です

于 2016-09-08T10:13:51.003 に答える
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

配列の長さ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 に答える