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
2008-10-30 07:40:05
Good suggestion, but problematic due to licensing issues.
Ciryon
2008-10-30 07:49:44
Won't perform the camel-case operation, though.
Ken Gentle
2008-10-30 12:10:25
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
2008-10-30 07:40:36
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
2008-10-30 12:12:17
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
2008-10-30 07:42:01