splitting strings in midlets

Discussion in 'Java' started by leni, Aug 6, 2005.

  1. leni

    leni Guest

    Hello,

    I'm a newbe in midlet programming and I need some help.
    My midlet communicates with a server to get certain information, for
    example something like "name|address|telefon number|".
    My question is what is the best method to split the text into strings
    to display it on the screen.

    Thank you in advance.
     
    leni, Aug 6, 2005
    #1
    1. Advertising

  2. leni

    Jeff Schwab Guest

    leni wrote:
    > Hello,
    >
    > I'm a newbe in midlet programming and I need some help.
    > My midlet communicates with a server to get certain information, for
    > example something like "name|address|telefon number|".
    > My question is what is the best method to split the text into strings
    > to display it on the screen.
    >
    > Thank you in advance.
    >


    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.regex.Pattern;

    public class RecordFormatter {
    static private PrintWriter out =
    new PrintWriter(System.out, true);

    Pattern delimiter;

    public static void main(String[] args) throws IOException {
    RecordFormatter formatter =
    new RecordFormatter(Pattern.compile("\\|"));
    String record = "name|address|telefon number|";

    out.println(formatter.format(record));
    }

    RecordFormatter(Pattern delimiter) {
    this.delimiter = delimiter;
    }

    String format(String record) {
    return delimiter.matcher(record).replaceAll("\n");
    }
    }
     
    Jeff Schwab, Aug 7, 2005
    #2
    1. Advertising

  3. Jeff Schwab wrote:
    > leni wrote:
    >
    >> Hello,
    >>
    >> I'm a newbe in midlet programming and I need some help.
    >> My midlet communicates with a server to get certain information, for
    >> example something like "name|address|telefon number|".
    >> My question is what is the best method to split the text into strings
    >> to display it on the screen.
    >> Thank you in advance.

    >
    > import java.util.regex.Pattern;


    In a Midlet?!

    You need to do something like:

    static Vector split(String str) {
    Vector split = new Vector();
    int start = 0;
    for (;;) {
    int end = str.indexOf('|', start);
    if (end == -1) {
    split.addElement(str.substring(start));
    return split;
    }
    split.addElement(str.substring(start, end));
    start = end + 1;
    }
    }

    (I have not so much as attempted to compile that.)

    Note, that doesn't handle any escaping you might want to do.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 7, 2005
    #3
  4. leni

    Dag Sunde Guest

    "leni" <> wrote in message
    news:...
    > Hello,
    >
    > I'm a newbe in midlet programming and I need some help.
    > My midlet communicates with a server to get certain information, for
    > example something like "name|address|telefon number|".
    > My question is what is the best method to split the text into strings
    > to display it on the screen.



    Far from optimal (Two loops), but it works...

    /**Splits a string into a list of strings
    * @param - source, String original string to be split
    * @param - splitAt, String to split at
    * @return - Returns a String array containing the sub elements
    */
    protected String[] split(String source, String splitAt)
    {
    int cElements = 0;
    for (int i = 0; i < source.length(); i++)
    {
    if(source.charAt(i) == splitAt.charAt(0))
    cElements+=1;
    }
    String tmpResult[] = new String[cElements+1];

    int prevIndex = 0;
    for(int i = 0; i <= cElements; i++)
    {
    int curIndex = source.indexOf(splitAt,prevIndex+1);
    if(curIndex == -1)
    curIndex = source.length();

    tmpResult = new String(source.substring(
    prevIndex !=0 ? prevIndex+1 : prevIndex , curIndex));
    prevIndex = curIndex;
    }

    return tmpResult;
    }

    ....
    String result[] = split("name|address|telefon number", "|");
    for (int i=0; i<result.length; i++)
    System.out.println(result);
    ....

    --
    Dag.
     
    Dag Sunde, Aug 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. elziko

    MIDlets

    elziko, Jul 18, 2003, in forum: Java
    Replies:
    2
    Views:
    558
    Wong Chee Wee
    Jul 18, 2003
  2. Gyro
    Replies:
    1
    Views:
    382
    Darryl Pierce
    Feb 26, 2005
  3. John Ericson
    Replies:
    0
    Views:
    427
    John Ericson
    Jul 19, 2003
  4. Mark
    Replies:
    0
    Views:
    442
  5. John Dibling
    Replies:
    0
    Views:
    417
    John Dibling
    Jul 19, 2003
Loading...

Share This Page