Domanda

Quale sarebbe una semplice implementazione di un metodo per convertire una stringa come " Ciao a tutti " a " ciaoThereTutto " ;. In JavaME il supporto per le operazioni di utilità String e StringBuffer è piuttosto limitato.

È stato utile?

Soluzione

Implementazione rapida primitiva. Non ho idea delle restrizioni di J2ME, quindi spero che vada bene o che dia qualche idea ...

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);

Puoi sostituire l'appendice maiuscola contorta con:

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

sebbene possa sembrare più hacker.

Altri suggerimenti

Con CDC, hai:

String.getBytes (); // per convertire la stringa in un array di byte String.indexOf (int ch); // per localizzare l'inizio delle parole String.trim (); // per rimuovere gli spazi

Per lettere minuscole / maiuscole è necessario aggiungere (sottrarre) 32.

Con questi elementi, puoi costruire il tuo metodo.

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;
}

Suggerimento:

Può essere se puoi port one libreria regexp su J2ME , puoi usarla per rimuovere gli spazi nella tua stringa ...

Prova il seguente codice

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;
}

Suggerirei il seguente semplice codice:

    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;

Lo farei così:

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();
}

controlla

import org.apache.commons.lang.WordUtils;

String camel = WordUtils.capitalizeFully('I WANT TO BE A CAMEL', new char[]{' '});

return camel.replaceAll(" ", "");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top