Java中大小写的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2375649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:42:45  来源:igfitidea点击:

Converting to upper and lower case in Java

javastring

提问by Arav

I want to convert the first character of a string to Uppercase and the rest of the characters to lowercase. How can I do it?

我想将字符串的第一个字符转换为大写,将其余字符转换为小写。我该怎么做?

Example:

例子:

String inputval="ABCb" OR "a123BC_DET" or "aBcd"
String outputval="Abcb" or "A123bc_det" or "Abcd"

采纳答案by paxdiablo

Try this on for size:

试试这个尺寸:

String properCase (String inputVal) {
    // Empty strings should be returned as-is.

    if (inputVal.length() == 0) return "";

    // Strings with only one character uppercased.

    if (inputVal.length() == 1) return inputVal.toUpperCase();

    // Otherwise uppercase first letter, lowercase the rest.

    return inputVal.substring(0,1).toUpperCase()
        + inputVal.substring(1).toLowerCase();
}

It basically handles special cases of empty and one-character string first and correctly cases a two-plus-character string otherwise. And, as pointed out in a comment, the one-character special case isn't needed for functionality but I still prefer to be explicit, especially if it results in fewer useless calls, such as substring to get an empty string, lower-casing it, then appending it as well.

它基本上首先处理空字符串和一个字符的特殊情况,否则正确处理两个加字符的字符串。而且,正如评论中指出的那样,功能不需要单字符的特殊情况,但我仍然更喜欢显式,特别是如果它导致更少的无用调用,例如获取空字符串的子字符串,小写它,然后附加它。

回答by Martin

String inputval="ABCb";
String result = inputval.substring(0,1).toUpperCase() + inputval.substring(1).toLowerCase();

Would change "ABCb" to "Abcb"

将“ABCb”更改为“Abcb”

回答by Bozho

WordUtils.capitalizeFully(str)from apache commons-langhas the exact semantics as required.

WordUtils.capitalizeFully(str)来自apache commons-lang具有所需的确切语义。

回答by Satvant Singh

/* This code is just for convert a single uppercase character to lowercase 
character & vice versa.................*/

/* This code is made without java library function, and also uses run time input...*/



import java.util.Scanner;

class CaseConvert {
char c;
void input(){
//@SuppressWarnings("resource")  //only eclipse users..
Scanner in =new Scanner(System.in);  //for Run time input
System.out.print("\n Enter Any Character :");
c=in.next().charAt(0);     // input a single character
}
void convert(){
if(c>=65 && c<=90){
    c=(char) (c+32);
    System.out.print("Converted to Lowercase :"+c);
}
else if(c>=97&&c<=122){
        c=(char) (c-32);
        System.out.print("Converted to Uppercase :"+c);
}
else
    System.out.println("invalid Character Entered  :" +c);

}


  public static void main(String[] args) {
    // TODO Auto-generated method stub
    CaseConvert obj=new CaseConvert();
    obj.input();
    obj.convert();
    }

}



/*OUTPUT..Enter Any Character :A Converted to Lowercase :a 
Enter Any Character :a Converted to Uppercase :A
Enter Any Character :+invalid Character Entered  :+*/

回答by Ellen Spertus

I consider this simpler than any prior correct answer. I'll also throw in javadoc. :-)

我认为这比任何先前的正确答案都简单。我也会加入javadoc。:-)

/**
 * Converts the given string to title case, where the first
 * letter is capitalized and the rest of the string is in
 * lower case.
 * 
 * @param s a string with unknown capitalization
 * @return a title-case version of the string
 */
public static String toTitleCase(String s)
{
    if (s.isEmpty())
    {
        return s;
    }
    return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
}

Strings of length 1 do not needed to be treated as a special case because s.substring(1)returns the empty string when shas length 1.

长度为 1 的字符串不需要被视为特殊情况,因为s.substring(1)s长度为 1时返回空字符串。

回答by Madhuka Dilhan

String a = "ABCD"

using this

使用这个

a.toLowerCase();

all letters will convert to simple, "abcd"
using this

所有字母都将 使用此转换为简单的“abcd”

a.toUpperCase()

all letters will convert to Capital, "ABCD"

所有字母都将转换为大写,“ABCD”

this conver first letter to capital:

这将第一个字母转换为大写:

a.substring(0,1).toUpperCase()

this conver other letter Simple

这个转换其他字母简单

a.substring(1).toLowerCase();

we can get sum of these two

我们可以得到这两者的总和

a.substring(0,1).toUpperCase() + a.substring(1).toLowerCase();

result = "Abcd"

结果 = "abcd"