Home page

Saturday, July 1, 2023

Convert String to different cases in JAVA

In Java, you can convert a String to different cases, such as lowercase or uppercase, using the following methods:

Converting to lowercase:

Using the toLowerCase() method: String lowercaseString = originalString.toLowerCase();

Converting to uppercase:

Using the toUpperCase() method: String uppercaseString = originalString.toUpperCase();

Here's an example that demonstrates how to convert a String to different cases:

public class StringCaseConversionExample {

    public static void main(String[] args) {

        String originalString = "Hello, World!";

        String lowercaseString = originalString.toLowerCase();

        System.out.println("Lowercase: " + lowercaseString);

        String uppercaseString = originalString.toUpperCase();

        System.out.println("Uppercase: " + uppercaseString);

    }

}

Output:
Lowercase: hello, world!
Uppercase: HELLO, WORLD!

In this example, we have an originalString that contains the text "Hello, World!".

We convert the originalString to lowercase using the toLowerCase() method and store it in the lowercaseString variable.

We convert the originalString to uppercase using the toUpperCase() method and store it in the uppercaseString variable.

The resulting lowercase and uppercase strings are printed to the console.

These methods are useful when you need to standardize the case of strings for comparison, formatting, or other purposes.

No comments:

Post a Comment