Changing the case of characters within a String


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

  • toUpperCase to convert all characters to upper case
  • toLowerCase to convert all characters to 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.

String str = "Tutor Joes";
 
String upper = str.toUpperCase();
String lower = str.toLowerCase();
 
System.out.println(string); // "Tutor Joes"
System.out.println(lower);  // "tutor joes"
System.out.println(upper);  // "TUTOR JOES"

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.


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:

Steps :

  • Input a String:
    • Prompt the user to input a string
  • Convert the String to a Character Array:
    • Use toCharArray() to convert the input string into an array of characters.
  • Input the Character to Be Searched:
    • Prompt the user to input the character they want to find and change the case of.
  • Search for the Character in the Array:
    • Iterate through the character array to find the specified character.
  • Change Case if Character is Found:
    • If the character is found
      • Check if the character is uppercase or lowercase.
      • If it's uppercase, add 32 to the ASCII code to convert it to lowercase.
      • If it's lowercase, subtract 32 from the ASCII code to convert it to uppercase.
      • Update the character in the array with the modified case.
  • Convert the Modified Array Back to a String:
    • Use String.valueOf() to convert the modified character array back to a string.
  • Output the Modified String:
    • Print the updated string with the changed character case.

An example of the code for the algorithm is:

Scanner scanner = new Scanner(System.in);
 
System.out.println("Enter the String");
String inputString = scanner.next();
 
char[] charArray = inputString.toCharArray();
 
System.out.println("Enter the character you are looking for");
System.out.println(inputString);
 
String charToSearch = scanner.next();
char searchChar = charToSearch.charAt(0);
 
for (int i = 0; i < inputString.length(); i++)
{
	if (charArray[i] == searchChar)
	{
		if (searchChar >= 'a' && searchChar <= 'z')
		{
			searchChar -= 32;
		}
		else if (searchChar >= 'A' && searchChar <= 'Z')
		{
			searchChar += 32;
		}
		charArray[i] = searchChar;
		break;
	}
}
 
inputString = String.valueOf(charArray);
System.out.println(inputString);

Basic Programs