【问题标题】:First char to upper case [duplicate]第一个字符转大写[重复]
【发布时间】:2012-07-12 23:22:27
【问题描述】:

可能重复:
How to upper case every first letter of word in a string?
Most efficient way to make the first character of a String lower case?

我想将字符串的第一个字母转换为大写。我正在尝试使用 JavaDocs 中描述的 replaceFirst(),但我不知道正则表达式是什么意思。

这是我目前尝试过的代码:

public static String cap1stChar(String userIdea)
{
    String betterIdea, userIdeaUC;
    char char1;
    userIdeaUC = userIdea.toUpperCase();
    char1 = userIdeaUC.charAt(0);
    betterIdea = userIdea.replaceFirst(char1); 
    return betterIdea;
}//end cap1stChar

编译器错误是参数列表的长度不同。我认为这是因为缺少正则表达式,但我不知道那到底是什么。

【问题讨论】:

标签: java string


【解决方案1】:

Regular Expressions(缩写为“regex”或“reg-ex”)是定义搜索模式的字符串。

replaceFirst() 所做的是使用参数中提供的正则表达式,并将搜索的第一个结果替换为您作为另一个参数传入的任何内容。

您要做的是使用String 类的charAt() 方法将字符串转换为数组,然后使用Character.toUpperCase() 将字符更改为大写(显然)。您的代码如下所示:

char first = Character.toUpperCase(userIdea.charAt(0));
betterIdea = first + userIdea.substring(1);

或者,如果您对更复杂的单行 java 代码感到满意:

betterIdea = Character.toUpperCase(userIdea.charAt(0)) + userIdea.substring(1);

两者都做同样的事情,将userIdea的第一个字符转换为大写字符。

【讨论】:

  • userIdea.toCharArray()[0] 是一种昂贵的方式来做userIdea.charAt(0)
  • @PeterLawrey 我不知道我是怎么忘记的。谢谢。
  • StringBuilder 类是很好的伴侣,假设名称是字符串而不是代码 => if(name.length()>0){ StringBuilder stringBuilder = new StringBuilder(name); if(stringBuilder.length()==1){ name= String.valueOf(Character.toUpperCase(stringBuilder.charAt(0))); }else{ name= String.valueOf(Character.toUpperCase(stringBuilder.charAt(0)))+stringBuilder.substring(1); } }
【解决方案2】:

或者你可以这样做

s = Character.toUpperCase(s.charAt(0)) + s.substring(1); 

【讨论】:

  • 不适用于短于两个字符的字符串。
  • @Thilo:是的,它适用于长度为 1 的字符串。
  • 为什么任何人都想获得空字符串第一个字符的大写等效项?
  • 也许那些不希望他们的程序由于意外的空字符串而崩溃的人;-)
  • @Dominik 你的意思是“意外的空字符串”吗? null 是另一种情况。
【解决方案3】:
public static String cap1stChar(String userIdea)
{
    char[] stringArray = userIdea.toCharArray();
    stringArray[0] = Character.toUpperCase(stringArray[0]);
    return userIdea = new String(stringArray);
}

【讨论】:

    【解决方案4】:

    编译错误是由于没有正确提供参数,replaceFirst 接受 regx 作为初始参数。 [a-z]{1} 将匹配长度为 1 的简单字母字符的字符串。

    试试这个。

    betterIdea = userIdea.replaceFirst("[a-z]{1}", userIdea.substring(0,1).toUpperCase())
    

    【讨论】:

      【解决方案5】:
      String toCamelCase(String string) {
          StringBuffer sb = new StringBuffer(string);
          sb.replace(0, 1, string.substring(0, 1).toUpperCase());
          return sb.toString();
      
      }
      

      【讨论】:

        【解决方案6】:
        userIdeaUC = userIdea.substring(0, 1).toUpperCase() + userIdea.length() > 1 ? userIdea.substring(1) : "";
        

        userIdeaUC = userIdea.substring(0, 1).toUpperCase();
        if(userIdea.length() > 1)
           userIdeaUC += userIdea.substring(1);
        

        【讨论】:

        • 可能想要删除三元表达式以提高 Matt B 的可读性。
        【解决方案7】:

        为了完整起见,如果您想使用 replaceFirst,请尝试以下操作:

        public static String cap1stChar(String userIdea)
        {
          String betterIdea = userIdea;
          if (userIdea.length() > 0)
          {
            String first = userIdea.substring(0,1);
            betterIdea = userIdea.replaceFirst(first, first.toUpperCase());
          }
          return betterIdea;
        }//end cap1stChar
        

        【讨论】:

          猜你喜欢
          • 2013-09-03
          • 1970-01-01
          • 2013-02-21
          • 2018-04-03
          • 1970-01-01
          • 1970-01-01
          • 2011-01-26
          • 2012-11-24
          • 2017-03-20
          相关资源
          最近更新 更多