简体   繁体   English

在java中使String首字母大写

[英]Make String first letter capital in java

As of now I'm using this code to make my first letter in a string capital截至目前,我正在使用此代码将我的第一个字母设为大写字符串

String output = input.substring(0, 1).toUpperCase() + input.substring(1);

This seems very dirty to me ..is there any direct or elegant way..这对我来说似乎很脏..有没有直接或优雅的方式..

How about this:这个怎么样:

String output = Character.toUpperCase(input.charAt(0)) + input.substring(1);

I can't think of anything cleaner without using external libraries, but this is definitely better than what you currently have.如果不使用外部库,我想不出任何更清洁的东西,但这绝对比你现在拥有的要好。

You should have a look at StringUtils class from Apache Commons Lang lib - it has method .capitalize()你应该看看来自Apache Commons Lang lib 的StringUtils类——它有方法.capitalize()

Description from the lib:来自库的描述:

Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char).根据 Character.toTitleCase(char) 将第一个字母更改为标题大小写的字符串大写。 No other letters are changed.其他字母没有变化。

String out = Character.toUpperCase(inText.charAt(0)) + inText.substring(1).toLowerCase();
public static void main(String[] args) {
    String str = null;
    String outStr = null;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String: ");
    str = sc.nextLine();
    //c= Character.toUpperCase(str.charAt(0));

    for(int i=0; i< (str.length());i++){

        if(str.charAt(i)==' '){

            outStr= outStr.substring(0,i+1)+str.substring(i+1,i+2).toUpperCase()+str.substring(i+2);

        }else if(i==0){

            outStr=str.substring(0,1).toUpperCase()+str.substring(1);

        }
    }
    System.out.println("STRING::"+outStr);
}

Assuming you can use Java 8, here's the functional way that nobody asked for...假设您可以使用 Java 8,这是没有人要求的功能方式...

import java.util.Optional;
import java.util.stream.IntStream;

public class StringHelper {
    public static String capitalize(String source) {
        return Optional.ofNullable(source)
            .map(str -> IntStream.concat(
                str.codePoints().limit(1).map(Character::toUpperCase),
                str.codePoints().skip(1)))
            .map(stream -> stream.toArray())
            .map(arr -> new String(arr, 0, arr.length))
            .orElse(null);
    }
}

It's elegant in that it handles the null and empty string cases without any conditional statements.它的优雅之处在于它无需任何条件语句即可处理空字符串和空字符串情况。

Character.toString(a.charAt(0)).toUpperCase()+a.substring(1)

PS = a 是字符串。

Here, hold my beer来, 拿着我的啤酒

String foo = "suresh";
String bar = foo.toUpperCase();
if(bar.charAt[0] == 'S'){
   throw new SuccessException("bar contains 'SURESH' and has the first letter capital").
}
class strDemo3
{
    public static void main(String args[])
    {
        String s1=new String(" the ghost of the arabean sea");
        char c1[]=new char[30];       
        int c2[]=new int[30];
        s1.getChars(0,28,c1,0);
        for(int i=0;i<s1.length();i++)
        {
           System.out.print(c1[i]);
        }
        for(int i=1;i<s1.length();i++)
        {
           c2[i]=c1[i];
           if(c1[i-1]==' ')
           {
               c2[i]=c2[i]-32;
           }
           c1[i]=(char)c2[i];          
        }
        for(int i=0;i<s1.length();i++)
        {
           System.out.print(c1[i]);
        }            
       }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM