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

Java String Case Conversion (toLowerCase() And toUpperCase())

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.

  1. Converting String to Lowercase in Java:

    • Using toLowerCase() method for converting a string to lowercase.
    String original = "Hello World";
    String lowercase = original.toLowerCase();
    
  2. Java Uppercase Conversion using toUpperCase():

    • Utilizing toUpperCase() method to convert a string to uppercase.
    String original = "Hello World";
    String uppercase = original.toUpperCase();
    
  3. Case-Insensitive String Comparison in Java:

    • Performing case-insensitive string comparison.
    String str1 = "Java";
    String str2 = "java";
    boolean isEqual = str1.equalsIgnoreCase(str2);
    
  4. Changing String Case in Java:

    • Changing the case of a string based on a condition.
    String input = "Java";
    if (condition) {
        input = input.toLowerCase();
    } else {
        input = input.toUpperCase();
    }
    
  5. Java String Case Conversion Examples:

    • Examples showcasing different scenarios of string case conversion.
    String mixedCase = "HeLLo WoRLd";
    String allLower = mixedCase.toLowerCase();
    String allUpper = mixedCase.toUpperCase();
    
  6. Using toLowerCase() for Case-Insensitive Comparisons:

    • Utilizing toLowerCase() for consistent case-insensitive comparisons.
    String userInput = getUserInput();
    if (userInput.toLowerCase().equals("exit")) {
        // Handle exit condition
    }
    
  7. Converting User Input to Lowercase in Java:

    • Converting user input to lowercase for uniform processing.
    String userInput = scanner.nextLine().toLowerCase();
    
  8. Java Case Conversion for Internationalization:

    • Handling case conversion in a locale-sensitive manner.
    String original = "ß";
    String uppercase = original.toUpperCase(Locale.GERMAN);
    
  9. Handling Case-Sensitive Operations in Java:

    • Addressing case sensitivity in string operations.
    String searchQuery = "Java";
    String document = "Java is a programming language.";
    if (document.contains(searchQuery)) {
        // Case-sensitive match found
    }
    
  10. Uppercase First Letter of a String in Java:

    • Capitalizing the first letter of a string.
    String input = "java";
    String capitalized = input.substring(0, 1).toUpperCase() + input.substring(1);
    
  11. Case Conversion in Java with Locale Support:

    • Ensuring proper case conversion considering locale.
    String original = "i";
    String capitalized = original.toUpperCase(Locale.ENGLISH);
    
  12. Transforming String Case in Java without Creating New Objects:

    • Efficiently transforming case without creating new string objects.
    StringBuilder mutable = new StringBuilder("Java");
    mutable.setCharAt(0, Character.toLowerCase(mutable.charAt(0)));
    
  13. Common Issues with toLowerCase() and toUpperCase() in Java:

    • Addressing potential pitfalls and common issues.
    String mixedCase = "HeLLo WoRLd";
    String incorrectLower = mixedCase.toLowerCase(); // Incorrect for locale-sensitive cases