Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, you can easily convert the case of a string using built-in methods provided by the String
class. This tutorial will cover two commonly used methods to convert strings to uppercase or lowercase:
1. toUpperCase()
The toUpperCase()
method is used to convert all characters in a string to uppercase. This method returns a new string with all the characters in uppercase, leaving the original string unchanged.
Example:
public class Main { public static void main(String[] args) { String text = "Hello, World!"; String upperText = text.toUpperCase(); System.out.println("Original: " + text); // Output: Hello, World! System.out.println("Uppercase: " + upperText); // Output: HELLO, WORLD! } }
In this example, we create a string text
and then call the toUpperCase()
method on it. The result is a new string with all characters in uppercase.
2. toLowerCase()
The toLowerCase()
method is used to convert all characters in a string to lowercase. This method returns a new string with all the characters in lowercase, leaving the original string unchanged.
Example:
public class Main { public static void main(String[] args) { String text = "Hello, World!"; String lowerText = text.toLowerCase(); System.out.println("Original: " + text); // Output: Hello, World! System.out.println("Lowercase: " + lowerText); // Output: hello, world! } }
In this example, we create a string text
and then call the toLowerCase()
method on it. The result is a new string with all characters in lowercase.
In this tutorial, we covered how to convert strings to uppercase or lowercase using the toUpperCase()
and toLowerCase()
methods provided by the String
class in Java. These methods allow you to easily change the case of a string while keeping the original string unchanged.
Converting String to Lowercase in Java:
toLowerCase()
method for converting a string to lowercase.String original = "Hello World"; String lowercase = original.toLowerCase();
Java Uppercase Conversion using toUpperCase()
:
toUpperCase()
method to convert a string to uppercase.String original = "Hello World"; String uppercase = original.toUpperCase();
Case-Insensitive String Comparison in Java:
String str1 = "Java"; String str2 = "java"; boolean isEqual = str1.equalsIgnoreCase(str2);
Changing String Case in Java:
String input = "Java"; if (condition) { input = input.toLowerCase(); } else { input = input.toUpperCase(); }
Java String Case Conversion Examples:
String mixedCase = "HeLLo WoRLd"; String allLower = mixedCase.toLowerCase(); String allUpper = mixedCase.toUpperCase();
Using toLowerCase()
for Case-Insensitive Comparisons:
toLowerCase()
for consistent case-insensitive comparisons.String userInput = getUserInput(); if (userInput.toLowerCase().equals("exit")) { // Handle exit condition }
Converting User Input to Lowercase in Java:
String userInput = scanner.nextLine().toLowerCase();
Java Case Conversion for Internationalization:
String original = "ß"; String uppercase = original.toUpperCase(Locale.GERMAN);
Handling Case-Sensitive Operations in Java:
String searchQuery = "Java"; String document = "Java is a programming language."; if (document.contains(searchQuery)) { // Case-sensitive match found }
Uppercase First Letter of a String in Java:
String input = "java"; String capitalized = input.substring(0, 1).toUpperCase() + input.substring(1);
Case Conversion in Java with Locale Support:
String original = "i"; String capitalized = original.toUpperCase(Locale.ENGLISH);
Transforming String Case in Java without Creating New Objects:
StringBuilder mutable = new StringBuilder("Java"); mutable.setCharAt(0, Character.toLowerCase(mutable.charAt(0)));
Common Issues with toLowerCase()
and toUpperCase()
in Java:
String mixedCase = "HeLLo WoRLd"; String incorrectLower = mixedCase.toLowerCase(); // Incorrect for locale-sensitive cases