J2ME: Replace parts of a string

C

cyberco

I'm porting code from J2SE to J2ME and one issue I run into is
replacing "\n\r" with " ". This is done in J2SE by:

String.replaceAll("a","b");

Unfortunately J2ME only offers a method for replacing chars:

String.replace('a','b');

Only thing I can come up with is a cumbersome solution while I get the
feeling there must be a more elegant solution.

Any ideas?
Thanks
 
D

Darryl Pierce

cyberco said:
I'm porting code from J2SE to J2ME and one issue I run into is
replacing "\n\r" with " ". This is done in J2SE by:

String.replaceAll("a","b");

Unfortunately J2ME only offers a method for replacing chars:

String.replace('a','b');

Only thing I can come up with is a cumbersome solution while I get the
feeling there must be a more elegant solution.

Any ideas?

Use what's in the MIDP. There's no other choice, since String is immutable.
 
R

Roedy Green

do you have a StringBuilder or StringBuffer?

StringBuilder n = new StringBuilder( s.length() );

for (int i=0; i<s.length; i++)
{

char c = s.charAt(i);
switch( c )
{
case '\r':
break;

case '\n':
n.append( ' ' );
break;

default:
n.append( c );
}
}

return n.toString();
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top