Thursday, July 1, 2010

Split string using delimiters

private String[] split(String original, String separator) {
    Vector nodes = new Vector();
    int index = original.indexOf(separator);
    while(index>=0) {
        nodes.addElement( original.substring(0, index) );
        original = original.substring(index+separator.length());
        index = original.indexOf(separator);
    }
    nodes.addElement( original );
    String[] result = new String[ nodes.size() ];
    if( nodes.size()>0 ) {
        for(int loop=0; loop
        result[loop] = (String)nodes.elementAt(loop);
    }
    return result;
}
    String ss=  "couples will randomly; appear " +
                "at several locations; across " +
                "the game screen.The; jumping " +
                "car will need to bump; into " +
                "these evil car; manifestations " +
                "using LEFT; or RIGHT keys to " +
                "defeatthem!; The more couples " +
                "that are; hit by the car in a" +
                "fixed; time the higher the score.;";

String[] result = split(ss, ";");
for(int i=0; i
        g.drawString(result[i],getWidth()>> 1,y,g.HCENTER | g.TOP);
        System.out.println(result[i]);
      
}

No comments:

Post a Comment