Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing CharSequence first letter to upper case in android

it may seem simple but it posses lots of bugs I tried this way:

 String s = gameList[0].toString();
s.replaceFirst(String.valueOf(s.charAt(0)),String.valueOf(Character.toUpperCase(s.charAt(0))) );

and it throws an exception

another try i had was :

String s = gameList[0].toString();
char c = Character.toUpperCase(gameList[0].charAt(0));
gameList[0] = s.subSequence(1, s.length());

rhis one also throws an Exception

like image 258
yoav.str Avatar asked Jun 23 '10 09:06

yoav.str


People also ask

How do you capitalize the first letter in Android?

If you are using an Android phone and Gboard, you can capitalize the first letter of any word with three touches. First, double-tap the word in question to highlight the word, then tap the shift button (the up arrow) on the keyboard to capitalize the first letter. Done!

How do you change a string into a uppercase?

The java. lang. Character. toUpperCase(char ch) converts the character argument to uppercase using case mapping information from the UnicodeData file.

How do you capitalize first letter only in Java?

The simplest way to capitalize the first letter of a string in Java is by using the String. substring() method: String str = "hello world!"; // capitalize first letter String output = str. substring(0, 1).

How do you change a character to a character sequence?

We can convert a char to a string object in java by using the Character. toString() method.


1 Answers

/**
 * returns the string, the first char lowercase
 *
 * @param target
 * @return
 */
public final static String asLowerCaseFirstChar(final String target) {

    if ((target == null) || (target.length() == 0)) {
        return target; // You could omit this check and simply live with an
                       // exception if you like
    }
    return Character.toLowerCase(target.charAt(0))
            + (target.length() > 1 ? target.substring(1) : "");
}

/**
 * returns the string, the first char uppercase
 *
 * @param target
 * @return
 */
public final static String asUpperCaseFirstChar(final String target) {

    if ((target == null) || (target.length() == 0)) {
        return target; // You could omit this check and simply live with an
                       // exception if you like
    }
    return Character.toUpperCase(target.charAt(0))
            + (target.length() > 1 ? target.substring(1) : "");
}
like image 131
Pentium10 Avatar answered Oct 31 '22 05:10

Pentium10