splitting strings in midlets

L

leni

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.
 
J

Jeff Schwab

leni said:
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");
}
}
 
T

Thomas Hawtin

Jeff said:
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
 
D

Dag Sunde

leni said:
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);
....
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top