[英]Converting to upper and lower case in Java
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"
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.
并且,正如评论中所指出的那样,功能不需要单字符特殊情况,但我仍然喜欢明确,特别是如果它导致更少的无用调用,例如子字符串以获得空字符串,更低的外壳它,然后附加它。
String a = "ABCD"
using this 用这个
a.toLowerCase();
all letters will convert to simple, "abcd" 所有字母都会转换成简单的“abcd”
using this 用这个
a.toUpperCase()
all letters will convert to Capital, "ABCD" 所有字母都将转换为Capital, “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”
来自apache commons-lang的 WordUtils.capitalizeFully(str)
具有所需的确切语义。
String inputval="ABCb";
String result = inputval.substring(0,1).toUpperCase() + inputval.substring(1).toLowerCase();
Would change "ABCb" to "Abcb" 将“ABCb”更改为“Abcb”
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 s
has length 1. 长度为1的字符串不需要被视为特殊情况,因为当
s
长度为1时, s.substring(1)
返回空字符串。
/* 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 :+*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.