Reputation: 5247
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"
Upvotes: 79
Views: 360224
Reputation: 1416
String a = "ABCD"
using this
a.toLowerCase();
all letters will convert to simple, "abcd"
using this
a.toUpperCase()
all letters will convert to 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"
Upvotes: 20
Reputation: 6805
I consider this simpler than any prior correct answer. I'll also throw in 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.
Upvotes: 5
Reputation: 881093
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.
Upvotes: 117
Reputation: 597006
WordUtils.capitalizeFully(str)
from apache commons-lang has the exact semantics as required.
Upvotes: 11
Reputation: 5
/* 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 :+*/
Upvotes: -5
Reputation: 7139
String inputval="ABCb";
String result = inputval.substring(0,1).toUpperCase() + inputval.substring(1).toLowerCase();
Would change "ABCb" to "Abcb"
Upvotes: 8