Society & Culture & Entertainment Hobbies & Science

How to Make a Single Letter Lower Case in Java

    • 1). Create a text string variable in your program. If you already have a text string you can use it, but to create a new one use the following syntax:

      String myText = "ABCDEFGHI";

      This string contains upper case characters for demonstration, but you can include any characters you like. Create an instance of the StringBuilder class using the following code:
      StringBuilder myBuilder = new StringBuilder(myText);

      The StringBuilder object takes an initial string to use for its processing, so the code passes the string variable reference to its constructor method.

    • 2). Get a reference to the character you wish to convert to lower case. Using the methods of the StringBuilder class, your program can get access to individual characters. Add the following code to your program:

      myBuilder.substring(0, 1)

      This code specifies the first character in the string, but you can alter the parameters to indicate the character you want. The first parameter indicates the index of the first character you want in the string, with the second indicating the position immediately after the section you want. The following code specifies the final character in the string:

      myBuilder.substring(myBuilder.length-1)

      When the substring method only receives one parameter, it runs from that position to the end of the string.

    • 3). Convert the character to lower case. Extend your substring code line as follows:

      myBuilder.substring(0, 1).toLowerCase();

      This converts the specified string section to lower case. Store the string section as a character by extending your code as follows:

      char lower = myBuilder.substring(0, 1).toLowerCase().charAt(0);

      The substring process creates a string, so you need to convert it to a character to store it as one. This is what the "charAt" call does, since the string only contains a single letter.

    • 4). Alter the string value to reflect the lower case operation. So far, the code has stored one of the string characters in a variable, converted to lower case. However, the original string remains unaffected. Add the following line of code to your program:

      myBuilder.setCharAt(0, lower);

      This code alters the value of the first character to reflect the new version. Alter the first parameter to suit the position of the letter you are converting to lower case.

    • 5). Retrieve the string value from your StringBuilder object. Add the following line of code, setting the value of the string variable to the altered version modeled by the StringBuilder:

      myText = myBuilder.toString();

      If you prefer, you can store the new value in a second variable rather than replacing the original one. You can test your string using the following code:

      System.out.println(myText);

      When you run your program you will see the new string value written to the console.

You might also like on "Society & Culture & Entertainment"

#

How to Carve Driftwood

#

Green Energy Utility Companies

#

Definition of Conductivity

#

How to Make One Finger Mittens

#

Homemade Squirt Guns

#

The Best Places for Stargazing

#

What Do Nematodes Eat?

#

How to Calculate ug/mL

#

Santa Fe Style Pot Throwing

Leave a reply