how to make a string lowercase in java

www.lautt‮moc.iru‬
how to make a string lowercase in java

To make a string lowercase in Java, you can use the toLowerCase method of the String class.

Here is an example of how you can use the toLowerCase method to make a string lowercase:

String input = "Hello, World!";
String output = input.toLowerCase();
// output is now "hello, world!"

In this example, the toLowerCase method is used to convert the input string to lowercase, and the resulting lowercase string is stored in the output variable.

You can also use the toLowerCase method in combination with other string manipulation methods, such as trim or replaceAll, to modify the string in more complex ways. For example, you can use the following code to make a string lowercase and remove leading and trailing white space:

String input = "  Hello, World!  ";
String output = input.toLowerCase().trim();
// output is now "hello, world!"

In this example, the toLowerCase method is used to convert the input string to lowercase, and the trim method is used to remove leading and trailing white space. The resulting lowercase string with no leading or trailing white space is stored in the output variable.

You can also use the replaceAll method to replace certain characters in the string with other characters, as shown in the following example:

String input = "Hello, World!";
String output = input.toLowerCase().replaceAll("[^a-z ]", "");
// output is now "hello world "

In this example, the toLowerCase method is used to convert the input string to lowercase, and the replaceAll method is used to replace all characters that are not in the range a-z or a space with an empty string. This results in a lowercase string with only alphabetic characters and spaces.

Keep in mind that the replaceAll method uses a regular expression to specify the characters to be replaced.

Created Time:2017-11-01 20:42:57  Author:lautturi