Uppercase or Lowercase a specific character in a word “Java”

纵然是瞬间 提交于 2019-12-25 11:51:27

问题


I have look into most of questions but I couldn't find how to uppercase or lowercase specific character inside a word.

Example:

String name = "Robert"

What if I would like to make "b" Uppercase and rest lowercase also how to make first letter Uppercase and rest lowercase?

Like "john" >> Output >> "John"...

I have toUppercase() and toLowercase(). They convert the whole text.

Also I tried to include charAt but never worked with me.


回答1:


You will need to take your string, take a substring of the specific character or characters you want to capitalize or lowercase, and then build a new string off of it.

Example

String test = "JoHn"; //make the H lowercase
test = test.substring(0,2) + test.substring(2,3).toLowercase() + test.substring(3);

The first substring gets all characters before the desired point, the second gets the desired character and lowercases it, and the final substring gets the rest of the string




回答2:


You can use toCharArray() to capitalize the first letter like this:

String name = "robert";

// Convert String to char array.
char[] arr = name.toCharArray();

// Modify first element in array.
arr[0] = Character.toUpperCase(arr[0]);
String str = new String(arr);
System.out.println(str);

Output:

Robert

And you want to make "b" Uppercase and rest lowercase like this:

// Convert String to char array.
char[] arr2 = name.toCharArray();

// Modify the third element in array.
arr2[2] = Character.toUpperCase(arr2[2]);
String str2 = new String(arr2);
System.out.println(str2);

Output:

roBert



回答3:


//Try this...

String str = "Robert";

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

    // you can directly use character instead of ascii codes
    if (aChar == 'b') {
        aChar = aChar - 32;
    } else if (aChar >= 'A' && aChar <= 'Z') {
        aChar += 32 ;
    }

    System.out.print((char) aChar);
}

/*
Output will be- roBert

*/




回答4:


I wouldn't use 'test.substring(2, 3).toLowerCase()' necessarily. 'Character.valueOf(test.charAt(2)).toUpperCase()' works. Also, the 'test.substring(0, 3)' is wrong; it should be 'test.substring(0, 2)'.




回答5:


A function that capitalize the first letter

private String capitalize(String str) {
    return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}


A function that capitalize an arbitrary letter

private String replaceCharWithUpperCase(char letterToCapitalize, String str)
{
    return str.replaceAll(letterToCapitalize, Character.toUpperCase(letterToCapitalize));
}

Then you can use the previous functions like that :

String a = "JOHN";
a = capitalize(a.toLowerCase());
// now a = John.

String b = "ROBERT";
a = replaceCharWithUpperCase('b', a.toLowerCase());
// now a = roBert.


来源:https://stackoverflow.com/questions/34538000/uppercase-or-lowercase-a-specific-character-in-a-word-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!