Hello all,
I am retrieving data from servlet to midlet. Now coming data is whole string. but now I want to chop it into different words. here my code is :

HttpConnection conn = (HttpConnection) Connector.open(url);
               StringBuffer sb = new StringBuffer();
               conn.setRequestMethod(HttpConnection.GET);
               conn.setRequestProperty("Content-Type", "Application/x-urlformencoded");
               InputStream is = conn.openInputStream();
               byte[] b = new byte[(int) conn.getLength()];
               int ch = 0;
               while ((ch = is.read()) != -1) {
                  sb.append((char) ch);
               }


               System.out.println(sb);

and the output is

LG

WIPRO

VIDEOCON

DELL

HP

COMPAQ

But now I want each word and append them into list. What can I do?

This page from rim gives examples of string utility functions for j2me, the first on the page (split) should do the trick

commented: Thanx for sharing +16

Thanks burgercho. Its really Helpful. Even I found one code.

public static String[] stringToArray(String a, String delimeter) {
      String c[] = new String[0];
      String b = a;
      while (true) {
         int i = b.indexOf(delimeter);
         String d = b;
         if (i >= 0) {
            d = b.substring(0, i);
         }
         String e[] = new String[c.length + 1];
         for (int k = 0; k < c.length; k++) {
            e[k] = c[k];
         }
         e[e.length - 1] = d;
         c = e;
         b = b.substring(i + delimeter.length(), b.length());
         if (b.length() <= 0 || i < 0) {
            break;
         }
      }
      return c;
   }

This is also working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.