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.
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.
To change the case of a specific character of an ASCII string following algorithm can be used:
Steps :
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);
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions