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

How to make the string into an array which length is 10? I expected the array is:

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]="";

Thanks very much!

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
stream
  • 369
  • 1
  • 7
  • 18
  • 1
    Please put your question in more understandable way. what's that long string-like thing at the top? – Mikayil Abdullayev Feb 21 '13 at 05:43
  • 1
    go through the java.lang.String library class. there are lot of methods to parse the string.[oracle link](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html) – Koushik Shetty Feb 21 '13 at 05:46
  • By your logic, `array[5]="Leaflets, samples";` should be more along the lines of `array[5]="\"Leaflets, samples\"";` – Andrew Thompson Feb 21 '13 at 05:47
  • You already split it. You just need to add array initialization. What's the problem? – default locale Feb 21 '13 at 05:48
  • @Bohemian: Only the answer by bestsss seems to address the escaping of quote inside a quoted string. Other answer just makes the implicit assumption that quote never appears inside a quoted string. – nhahtdh Feb 21 '13 at 05:59

3 Answers3

3

You are looking for CSV reader. You can use opencsv.

With opencsv library:

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

It returns an array of column values.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
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 + "'");
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • One potential problem with this solution is that it assumes that quote never appears inside quoted string (escaping of quote not taken into consideration). (By the way, why do you add a space before the arguments to function call?). – nhahtdh Feb 21 '13 at 06:07
  • @nhahtdh This is a problem, but missing feature which is easy to add. – Mikhail Vladimirov Feb 21 '13 at 06:37
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);
asifsid88
  • 4,631
  • 20
  • 30