JavaME: Convertir une chaîne en camelCase
-
05-07-2019 - |
Question
Qu'est-ce qu'une simple implémentation d'une méthode pour convertir une chaîne telle que "Bonjour à tous" pour "BonjourThereEveryone". Dans JavaME, la prise en charge des opérations String et StringBuffer est assez limitée.
La solution
Implémentation rapide primitive. Je n’ai aucune idée des restrictions de J2ME, alors j’espère que ça ira ou que ça donne quelques idées ...
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);
Vous pouvez remplacer l’ajout majuscule compliqué par:
result.append((char) (c - 0x20));
bien que cela puisse sembler plus hackish.
Autres conseils
Avec CDC, vous avez:
String.getBytes (); // pour convertir la chaîne en un tableau d'octets String.indexOf (int ch); // pour localiser le début des mots String.trim (); // pour supprimer des espaces
Pour les majuscules / minuscules, vous devez ajouter (soustraire) 32.
Avec ces éléments, vous pouvez créer votre propre méthode.
private static String toCamelCase(String s) {
String result = "";
String[] tokens = s.split("_"); // or whatever the divider is
for (int i = 0, L = tokens.length; i<L; i++) {
String token = tokens[i];
if (i==0) result = token.toLowerCase();
else
result += token.substring(0, 1).toUpperCase() +
token.substring(1, token.length()).toLowerCase();
}
return result;
}
Suggestion:
Peut-être si vous pouvez porter un bibliothèque regexp sur J2ME , vous pouvez l’utiliser pour supprimer des espaces dans votre chaîne ...
Essayez le code suivant
public static String toCamel(String str) {
String rtn = str;
rtn = rtn.toLowerCase();
Matcher m = Pattern.compile("_([a-z]{1})").matcher(rtn);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
rtn = sb.toString();
return rtn;
}
Je suggérerais le code simple suivant:
String camelCased = "";
String[] tokens = inputString.split("\\s");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
camelCased = camelCased + token.substring(0, 1).toUpperCase() + token.substring(1, token.length());
}
return camelCased;
Je le ferais comme ceci:
private String toCamelCase(String s) {
StringBuffer sb = new StringBuffer();
String[] x = s.replaceAll("[^A-Za-z]", " ").replaceAll("\\s+", " ")
.trim().split(" ");
for (int i = 0; i < x.length; i++) {
if (i == 0) {
x[i] = x[i].toLowerCase();
} else {
String r = x[i].substring(1);
x[i] = String.valueOf(x[i].charAt(0)).toUpperCase() + r;
}
sb.append(x[i]);
}
return sb.toString();
}
cochez cette
import org.apache.commons.lang.WordUtils;
String camel = WordUtils.capitalizeFully('I WANT TO BE A CAMEL', new char[]{' '});
return camel.replaceAll(" ", "");