Site icon TechVidvan

Java String toLowerCase() Method with Examples

java String toLowerCase()

When all the letters in a string need to be changed to lowercase, the Java String toLowerCase() method is utilized. Using the locale’s guidelines, the characters are converted to lowercase.

Similar to the toLowerCase(Locale.getDefault()) method, the toLowerCase() method lowers the case. Internally, it takes advantage of the system locale.

Note: The toUpperCase() function converts a string to uppercase letters.

Syntax

public String toLowerCase()

Java String toLowerCase() Methods

The following are the two different toLowerCase() methods:

The java string toLowerCase() function returns lowercase letters. In other words, it changes every character in the string to a lowercase letter.

Similar to the toLowerCase(Locale.getDefault()) method, the toLowerCase() method lowers the case. Internally, it takes advantage of the system locale.

Example:

Public class TechVidvan
 {
  public static void main(String[] args)
 {
    String str1 = "JAVA PROGRAMMING";

    // convert to lower case letters
    System.out.println(str1.toLowerCase());

  }
}
Output:
java programming

Parameters for toLowerCase()

No parameters are required for the toLowerCase() function.

The toLowerCase() Return

Value produces a string that converts all capital letters to lowercase characters.

Available Signatures

Java String toLowerCase​() Locale Examples

Internal conversion is performed via toLowerCase() based on the computer’s or server’s default locale.

toLowerCase(Locale.getDefault());

How to use JavaScript to capitalize only the first letter of a string
What happens if you only want the first letter in a string to be capitalized?

Here is a short example that demonstrates one approach to accomplishing that.

Let’s say that the variable’s string value called myGreeting is hello, all lowercase.

let myGreeting be 'hi';

One approach to accomplishing this is the slice() method. This generates a new string beginning at the supplied index and continuing through the end of the word.

From the second letter to the value’s conclusion, you should begin.

Since index 1 is the index of the second letter in this situation, that should be the input you send to slice().

In this manner, the initial character is wholly disregarded. It is removed from the string and replaced with a new one that still contains the remaining characters without the first letter.

Afterwards, save the procedure to a new variable.

let slice(1) of myGreeting be restOfGreeting;

‘ello’ is returned by the command console.log(restOfGreeting);

Example:

function capFirst(str) {
     return str[0].toUpperCase() + str.slice(1);
 }

console.log(capFirst('hello'));

output
Hello

Converting Upper Case string to lower case

Uppercase letters can be converted to lowercase by changing their ASCII value.

-->java.util Package
    --> String Class
        --> toLowerCase() Method

Similar to the toLowerCase() method, the toUpperCase() method changes the string value to uppercase.

The following is the general syntax for calling the method:

String.toUpper()

No parameters are entered. The value of the string given by the toLowerCase() method is unaltered since strings in JavaScript are immutable.

ToLower() returns a lowercase version of this string that has been copied.
ToLower(CultureInfo) Returns a copy of this string that has been changed to lowercase according to the culture that was supplied

Example:

using System;

public class Techvidvan {
    public static void Main() {

        string [] info = {"Name", "Title", "Age", "Location", "Gender"};

        Console.WriteLine("The initial values in the array are:");
        foreach (string s in info)
            Console.WriteLine(s);

        Console.WriteLine("{0}The lowercase of these values is:", Environment.NewLine);

        foreach (string s in info)
            Console.WriteLine(s.ToLower());

        Console.WriteLine("{0}The uppercase of these values is:", Environment.NewLine);

        foreach (string s in info)
            Console.WriteLine(s.ToUpper());
    }
}

The example displays the following output:

The initial values in the array are:
Name
Title
Age
Location
Gender

The lowercase of these values is:
name
title
age
location
gender

The uppercase of these values is:
NAME
TITLE
AGE
LOCATION
GENDER

Lowercase Strings in Java Have the Following Benefits:

Lowercase strings preserve your code’s consistency since they adhere to standard Java name rules.

It makes it less likely that classes, methods, and variables will have similar names, but different cases will be confused. Java follows the tradition of using camelCase rather than UpperCamelCase for variables, methods, and class names. Strings should be written in lowercase to avoid conflicting with these norms.

Lowercase strings are typically more portable across many programming languages and systems. Using lowercase consistently helps stop problems caused by case sensitivity on some platforms, which may treat filenames or URLs differently depending on the case.

Using lowercase strings in Java has some drawbacks

Information Loss

Lowercase conversion of strings may lead to information loss. Lowercase may not be appropriate if the input’s original case needs to be preserved.

Reduced Readability

Lowercase strings occasionally may be more complex to understand, particularly for acronyms or abbreviations. For instance, when dealing with code linked to HTTP, “HTTPRequest” is more legible than “httprequest”.

Limited Use Cases

Lowercase strings might not be appropriate in every circumstance. Maintaining the original case is crucial when working with proper nouns or natural language processing.

Conclusion

Finally, lowercase strings in Java have various benefits, mainly in preserving code integrity and averting naming conflicts. They comply with standard Java naming conventions, enhancing code readability and understanding.

Additionally, they enhance portability by reducing the risk of case sensitivity issues on other systems. It’s vital to remember that not all circumstances require lowercase strings, mainly when dealing with acronyms and proper names or when maintaining the input’s original case is required. Because of this, the decision to use lowercase or another case strategy should be based on the particular needs of your code as well as readability issues for your target audience.

Exit mobile version