Changing the case of characters within a String

suggest change

The String type provides two methods for converting strings between upper case and lower case:

These methods both return the converted strings as new String instances: the original String objects are not modified because String is immutable in Java. See this for more on immutability : Immutability of Strings in Java

String string = "This is a Random String";
String upper = string.toUpperCase();
String lower = string.toLowerCase();

System.out.println(string);   // prints "This is a Random String"
System.out.println(lower);    // prints "this is a random string"
System.out.println(upper);    // prints "THIS IS A RANDOM STRING"

Non-alphabetic characters, such as digits and punctuation marks, are unaffected by these methods. Note that these methods may also incorrectly deal with certain Unicode characters under certain conditions.

Note: These methods are locale-sensitive, and may produce unexpected results if used on strings that are intended to be interpreted independent of the locale. Examples are programming language identifiers, protocol keys, and HTML tags.

For instance, "TITLE".toLowerCase() in a Turkish locale returns “tıtle”, where ı (\u0131) is the LATIN SMALL LETTER DOTLESS I character. To obtain correct results for locale insensitive strings, pass Locale.ROOT as a parameter to the corresponding case converting method (e.g. toLowerCase(Locale.ROOT) or toUpperCase(Locale.ROOT)).

Although using Locale.ENGLISH is also correct for most cases, the language invariant way is Locale.ROOT.

A detailed list of Unicode characters that require special casing can be found on the Unicode Consortium website.

Changing case of a specific character within an ASCII string:

To change the case of a specific character of an ASCII string following algorithm can be used:


  1. Declare a string.
  2. Input the string.
  3. Convert the string into a character array.
  4. Input the character that is to be searched.
  5. Search for the character into the character array.
  6. If found,check if the character is lowercase or uppercase.
* If Uppercase, add 32 to the ASCII code of the character.
* If Lowercase, subtract 32 from the ASCII code of the character.
  1. Change the original character from the Character array.
  2. Convert the character array back into the string.

Voila, the Case of the character is changed.

An example of the code for the algorithm is:

Scanner scanner = new Scanner(;
System.out.println("Enter the String");
String s =;
char[] a = s.toCharArray();
System.out.println("Enter the character you are looking for");
String c =;
char d = c.charAt(0);

for (int i = 0; i <= s.length(); i++) {
    if (a[i] == d) {
        if (d >= 'a' && d <= 'z') {
            d -= 32;
        } else if (d >= 'A' && d <= 'Z') {
            d += 32;
        a[i] = d;
s = String.valueOf(a);

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents