0
5163583,601028,30,,0,"Leaflets, samples",Cycle 5 objectives,,20100804T071410,

文字列を長さが10の配列にする方法は?配列は次のとおりです。

array[0]="5163583";
array[1]="601028";
array[2]="30";
array[3]="";
array[4]="0";
array[5]="Leaflets, samples";
array[6]="Cycle 5 objectives";
array[7]="";
array[8]="20100804T071410";
array[9]="";

どうもありがとう!

4

3 に答える 3

3

CSVリーダーを探しています。opencsvを使用できます。

opencsvライブラリの場合:

new CSVReader(new StringReader(inputString)).readNext()

列値の配列を返します。

于 2013-02-21T06:01:09.407 に答える
1
String string = 
    "5163583,601028,30,,0,\"Leaflets, samples\",Cycle 5 objectives,,20100804T071410,";

Matcher m = Pattern.compile ("(\"[^\"]*\"|[^,\"]*)(?:,|$)").matcher (string);

List <String> chunks = new ArrayList <String> ();
while (m.find ())
{
    String chunk = m.group (1);
    if (chunk.startsWith ("\"") && chunk.endsWith ("\""))
        chunk = chunk.substring (1, chunk.length () - 1);
    chunks.add (chunk);
}

String array [] = chunks.toArray (new String [chunks.size ()]);
for (String s: array)
    System.out.println ("'" + s + "'");
于 2013-02-21T05:42:01.923 に答える
0
String sb = "5163583,601028,30,,0,\"Leaflets, samples\",Cycle 5 objectives,,20100804T071410,";

String[] array = new String[10];
StringBuilder tmp = new StringBuilder();
int count=0;
for(int i=0, index=0; i<sb.length(); i++)
{
    char ch = sb.charAt(i);
    if(ch==',' && count==0)
    {
        array[index++] = tmp.toString();
        tmp = new StringBuilder();
        continue;
    }
    else if(ch=='"')
    {
        count = count==0 ? 1 : 0;
        continue;
    }

    tmp.append(ch);
}
for(String s : array)
    System.out.println(s);
于 2013-02-21T06:13:51.403 に答える