10 Mar 2012

Split a string into array by a character delimiter [Java]

JavaME actually as there could be a string method to do this in Java (which I doubt though). Anyways, the objective is to split a string into array by a character delimiter, like the explode function in php.

Here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private String[] charSplit(char delimiter, String subject)
{
    Vector chunkStore = new Vector();
    String chunk = "";
    char [] subjectChars = subject.toCharArray();

    for(int i=0;i<subjectChars.length;i++)
    {
        if (subjectChars[i] == delimiter)
        {
            chunkStore.addElement(chunk);
            chunk = "";
        }
        else
            chunk += subjectChars[i];
    }
    chunkStore.addElement(chunk);
    
    String [] pieces = new String[chunkStore.size()];
    chunkStore.copyInto(pieces);

    return pieces;
}

Example

Working on the Prowork mobile app, I have to convert a date field (from the due date for a new task) to mysql format for the (unofficial) api. Unfortunately the string representation of date from the datefield in J2ME is dow mon dd hh:mm:ss zzz yyyy and to convert this mysql's yyyy-mm-dd hh:mm:ss I need to split this string by space and join together the pieces in the wanted order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Return date in mysql format: yyyy-mm-dd hh:mm:ss
private String formatDate(Date date)
{
    // Convert the date to string:
    // i.e dow mon dd hh:mm:ss zzz yyyy
    String dateString = date.toString();
    // Split by space
    String [] dateObjects = charSplit(' ', dateString);
    // so we have [0] = dow, [1] = mon, [2] = dd, [3] hh:mm:ss, etc

    // Get month index from month (dateObjects[1])
    // --> We need to convert the returned mnt from say Dec to 12
    // There are/should be better ways to do this
    String [] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    int monthIndex = 0;
    for(int i=0;i<months.length;i++)
    {
        if (dateObjects[1].equalsIgnoreCase(months[i]))
        {
            monthIndex = i+1;
            break;
        }
    }
    // Put the preceeding 0 (for 01 to 09) if need be 
    String month = monthIndex > 9 ? ""+monthIndex : "0"+monthIndex;
    
    String formattedDate = dateObjects[5]+"-"+month+"-"+dateObjects[2]+" "+dateObjects[3];
    //System.out.println(formattedDate);
    return formattedDate;
}

My name is Opeyemi Obembe. I build things for web and mobile. You should follow me on Twitter (@kehers).