JavaME:将字符串转换为驼峰命名法

发布于 2024-07-08 07:16:56 字数 105 浏览 15 评论 0原文

将“大家好”之类的字符串转换为“helloThereEveryone”的方法的简单实现是什么? 在 JavaME 中,对 String 和 StringBuffer 实用程序操作的支持非常有限。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

ゞ花落谁相伴 2024-07-15 07:16:57

我建议使用以下简单的代码:

    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;

I would suggest the following simple code:

    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;
聚集的泪 2024-07-15 07:16:57

我会这样做:

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

I would do it like this:

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();
}
揪着可爱 2024-07-15 07:16:57

检查这个

import org.apache.commons.lang.WordUtils;

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

return camel.replaceAll(" ", "");

check this

import org.apache.commons.lang.WordUtils;

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

return camel.replaceAll(" ", "");
无敌元气妹 2024-07-15 07:16:56

快速原始实现。 我不知道 J2ME 的限制,所以我希望它适合或者它能提供一些想法...

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

您可以将复杂的大写附加替换为:

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

尽管它可能看起来更黑客。

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.

人间☆小暴躁 2024-07-15 07:16:56

使用 CDC,您可以:

String.getBytes();//将字符串转换为字节数组
String.indexOf(int ch); //用于定位单词的开头
String.trim();//删除空格

对于小写/大写,您需要添加(减去) 32.

使用这些元素,您可以构建自己的方法。

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.

勿忘心安 2024-07-15 07:16:56
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;
}
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;
}
£冰雨忧蓝° 2024-07-15 07:16:56

建议:

如果您可以 移植一个正则表达式,可能是这样J2ME 上的库,您可以使用它来去除字符串中的空格...

Suggestion:

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

心清如水 2024-07-15 07:16:56

尝试以下代码

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

Try following code

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文