0

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Shah91n
  • 432
  • 1
  • 4
  • 10

5 Answers5

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

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
bhooks
  • 409
  • 5
  • 16
1

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
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1
//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

*/

Zain Aftab
  • 703
  • 7
  • 21
Pralhad
  • 11
  • 2
  • You need to check ASCII value for input characters. – Pralhad Jan 29 '18 at 11:11
  • You need to check ASCII value for input characters. In above example i have checked ascii values from a to z (96 to 123) plus i have check if any character ascii value matches to 97 (ASCII value of char 'a') and converted that character 'a' to 'A' by dedicating/mins 32. – Pralhad Jan 29 '18 at 11:18
0

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)'.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
0

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.
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59