【问题标题】:Get first letter of a String (more than one word)获取字符串的第一个字母(多个单词)
【发布时间】:2014-07-30 12:00:12
【问题描述】:

我正在尝试获取字符串中每个单词的第一个字母:

String recInf = recursos.getString(nombre);

char[] tipoAbreviado = recInf.toCharArray();
tipoAbreviado[0] = Character.toUpperCase(tipoAbreviado[0]);

for (int i = 0; i < recInf.length() - 2; i++) {
    // Es 'palabra'
    if (tipoAbreviado[i] == ' ' || tipoAbreviado[i] == '.' || tipoAbreviado[i] == ',') {
        // Reemplazamos
        tipoAbreviado[i + 1] = Character.toUpperCase(tipoAbreviado[i + 1]); 
    }

    nombre = tipoAbreviado.toString();
}

最后nombre的值是[C@3b1938ea,不是recInf中每个单词的首字母

【问题讨论】:

  • 使用静态方法Arrays.toString(tipoAbreviado) 而不是tipoAbreviado.toString()。数组的 toString 方法不会创建“可理解的”String

标签: java string char tostring


【解决方案1】:

您可以使用Arrays.toSting(your_array) 打印您的Array

看看toString()Arrays做什么

 public static String toString(long[] a) {
    if (a == null)
        return "null";
    int iMax = a.length - 1;
    if (iMax == -1)
        return "[]";

    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++) {
        b.append(a[i]);
        if (i == iMax)
            return b.append(']').toString();
        b.append(", ");
    }
} 

但是当你使用tipoAbreviado.toString(); 时,它会调用Object 类中的toString() 方法。

Object 类中的 toString() 方法有什么作用?

 public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

这就是为什么你要得到你当前的输出。

【讨论】:

    【解决方案2】:

    您应该使用 new String(char[])char[] 创建 String,而不是在 Array 上使用 toString prints the memory address representation

    nombre = new String(tipoAbreviado);
    

    【讨论】:

      【解决方案3】:

      您可以将字符串的.split() 与正确的正则表达式一起使用,然后选择第一个字符:

       String[] words = "Your String & !+ and some extraodrinary others".split("[^a-zA-Z]+");
       for (String word: words ){
          System.out.println(word.charAt(0));
       }
      

      【讨论】:

        【解决方案4】:

        您可以尝试使用正则表达式获取它们。

        当我在字符串Hallo Welt, ich bin ein String 上使用正则表达式(\w)\w+ 时,我得到一个带有H, W, i, b, e, S 的数组。注意:您必须将表达式作为全局表达式运行(不止一次)。

        【讨论】:

          【解决方案5】:

          假设您要处理的字符串对象是recInf,我建议按如下方式进行:

          String recInf = new String(text.replaceAll("[^\\p{L}\\p{Nd} ]+"," ")
              .replaceAll("[\\s]+", " "));
          String[] words = recInf.split(" ");
          String[] firstLetters = new String[words.length];
          
          for(int i=0; i<words.length;i++){
             firstLetters[i]=words[i].getCharAt(0);
          }
          

          【讨论】:

            【解决方案6】:

            你可以试试这个

            nombre = (new StringBuilder(tipoAbreviado)).toString();
            

            【讨论】:

            • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
            • "finally the value of nombre is [C@3b1938ea, not the first letter of each word in recInf" 我发现这句话是问题,所以我提供了一个解决方案。简而言之……
            猜你喜欢
            • 1970-01-01
            • 2013-09-04
            • 1970-01-01
            • 1970-01-01
            • 2013-08-24
            • 2019-01-16
            • 1970-01-01
            • 1970-01-01
            • 2018-07-11
            相关资源
            最近更新 更多