views:

2452

answers:

3

What would be a simple implementation of a method to convert a String like "Hello there everyone" to "helloThereEveryone". In JavaME support for String and StringBuffer utility operations are quite limited.

A: 

Suggestion:

May be if you can port one regexp library on J2ME, you could use it to strip spaces in your String...

VonC
Good suggestion, but problematic due to licensing issues.
Ciryon
Won't perform the camel-case operation, though.
Ken Gentle
A: 

Quick primitive implementation. I have no idea of restrictions of J2ME, so I hope it fits or it gives some ideas...

String str = "Hello, there, everyone?";

StringBuffer result = new StringBuffer(str.length());
String strl = str.toLowerCase();
boolean bMustCapitalize = false;
for (int i = 0; i < strl.length(); i++)
{
  char c = strl.charAt(i);
  if (c >= 'a' && c <= 'z')
  {
    if (bMustCapitalize)
    {
      result.append(strl.substring(i, i+1).toUpperCase());
      bMustCapitalize = false;
    }
    else
    {
      result.append(c);
    }
  }
  else
  {
    bMustCapitalize = true;
  }
}
System.out.println(result);

You can replace the convoluted uppercase append with:

result.append((char) (c - 0x20));

although it might seem more hackish.

PhiLho
Good solution, thank you!
Ciryon
Some J2ME implementations have extensions for alternate character sets. If this is a concern, you really need to follow the platform's recommendations for collation, so the char math trick won't work on those character sets.
Ken Gentle
A: 

With CDC, you have:

String.getBytes();//to convert the string to an array of bytes String.indexOf(int ch); //for locating the beginning of the words String.trim();//to remove spaces

For lower/uppercase you need to add(subtract) 32.

With these elements, you can build your own method.

michelemarcon