我使用Java从用户获得一个字符串输入。我试着让输入的第一个字母大写。

我试了一下:

String name;

BufferedReader br = new InputStreamReader(System.in);

String s1 = name.charAt(0).toUppercase());

System.out.println(s1 + name.substring(1));

这导致了以下编译错误:

类型不匹配:不能从InputStreamReader转换为BufferedReader 不能在基本类型char上调用toUppercase()


当前回答

下面的例子也将特殊字符后的单词大写,如[/-]

  public static String capitalize(String text) {
    char[] stringArray = text.trim().toCharArray();
    boolean wordStarted = false;
    for( int i = 0; i < stringArray.length; i++) {
      char ch = stringArray[i];
      if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '\'') {
        if( !wordStarted ) {
          stringArray[i] = Character.toUpperCase(stringArray[i]);
          wordStarted = true;
        } 
      } else {
        wordStarted = false;
      }
    }
    return new String(stringArray);
  }

Example:
capitalize("that's a beautiful/wonderful life we have.We really-do")

Output:
That's A Beautiful/Wonderful Life We Have.We Really-Do

其他回答

import java.util.*;
public class Program
{
    public static void main(String[] args) 
      {
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String[] s2=s1.split(" ");//***split text into words***
        ArrayList<String> l = new ArrayList<String>();//***list***
        for(String w: s2)
        l.add(w.substring(0,1).toUpperCase()+w.substring(1)); 
        //***converting 1st letter to capital and adding to list***
        StringBuilder sb = new StringBuilder();//***i used StringBuilder to convert words to text*** 
        for (String s : l)
          {
             sb.append(s);
             sb.append(" ");
          }
      System.out.println(sb.toString());//***to print output***
      }
}

我已经使用split函数将字符串分割成单词,然后我再次使用list来获得该单词的第一个字母大写,然后我使用字符串生成器以字符串格式打印输出,其中包含空格

System.out.println(Character.toString(A.charAt(0)).toUpperCase()+A.substring(1));

P.S = a是一个字符串。

这是我这边的解决方案,所有条件都检查过了。

   import java.util.Objects;

public class CapitalizeFirstCharacter {

    public static void main(String[] args) {

        System.out.println(capitailzeFirstCharacterOfString("jwala")); //supply input string here
    }

    private static String capitailzeFirstCharacterOfString(String strToCapitalize) {

        if (Objects.nonNull(strToCapitalize) && !strToCapitalize.isEmpty()) {
            return strToCapitalize.substring(0, 1).toUpperCase() + strToCapitalize.substring(1);
        } else {
            return "Null or Empty value of string supplied";

        }

    }

}

来自Ameen Mahheen的答案很好,但如果我们有一些双空格字符串,如“hello world”,那么sb.append得到IndexOutOfBounds异常。正确的做法是在这行之前测试,做:

private String capitalizer(String word){
        String[] words = word.split(" ");
        StringBuilder sb = new StringBuilder();
        if (words[0].length() > 0) {
            sb.append(Character.toUpperCase(words[0].charAt(0)) + words[0].subSequence(1, words[0].length()).toString().toLowerCase());
            for (int i = 1; i < words.length; i++) {
                sb.append(" ");
                if (words[i].length() > 0) sb.append(Character.toUpperCase(words[i].charAt(0)) + words[i].subSequence(1, words[i].length()).toString().toLowerCase());
            }
        }
        return  sb.toString();
    }

在Android Studio中

将此依赖项添加到构建中。gradle(模块:app)

dependencies {
    ...
    compile 'org.apache.commons:commons-lang3:3.1'
    ...
}

现在你可以使用

String string = "STRING WITH ALL CAPPS AND SPACES";

string = string.toLowerCase(); // Make all lowercase if you have caps

someTextView.setText(WordUtils.capitalize(string));