How to Replace Lowercase With Capitals in Java

How to Replace Lowercase With Capitals in Java thumbnail
Java strings often process user-input text.

Java programs model sequences of text characters as strings. Strings can store characters and carry out operations on them. The Java string class defines the methods programs can use on any strings they have declared. String methods cover many of different types of processing, including processes to alter the content of strings. If you have a Java program with a string in it, you can easily change the lowercase characters to uppercase using only a small amount of code.

Instructions

    • 1

      Prepare your string within your Java program. If you do not have a string yet, declare and instantiate one using the following syntax:

      String myWords = "Here are some words";

      You can use your own variable name and text characters when you create your own string variable. Using meaningful variable names makes programs easier to work with, so this is ideal where possible. Include any text characters you like within the string value.

    • 2

      Convert your text characters to uppercase. Using the "toUpperCase" method of the string class, convert all of the characters within your string to uppercase as follows:

      myWords.toUpperCase();

      This method takes the value of the string object calling it, then iterates through it, replacing any lowercase alphabetical characters with their uppercase counterparts. The method only affects alphabetical characters, so any punctuation symbols or numbers are left unaffected, and any uppercase characters already appearing in the string are left as they are.

    • 3

      Test your string value. In Java, strings are immutable, which means that any modification you perform on a string value actually results in the creation of a new string. When you call the "toUpperCase" method on a string, the string calling the method retains a copy of the original string value, converting to upper case within a new string. The operation therefore leaves the original string unaffected. Test your original string as follows:

      System.out.println(myWords);

      You should see the string appearing within your output area, still in its original state.

    • 4

      Save your new string in a variable. To acquire a reference to your new string value with the letters converted to uppercase, amend your code line as follows:

      String myUpperCaseWords = myWords.toUpperCase();

      The "toUpperCase" method returns a new string object containing the same characters as the original string, but with all uppercase letters. If you don't need the string in its original state, you can optionally reassign the new value to the original variable reference, overwriting its existing value:

      myWords = = myWords.toUpperCase();

    • 5

      Save your program, then test your code by compiling and running the program. Output the new value using the following syntax:

      System.out.println(myUpperCaseWords);

      You should see your string written out with all uppercase letters. If the code does not function as you expected it to, check your syntax and compile it again. Use your new string variable for whatever processing needs your program has.

Tips & Warnings

  • You can also use the "toLowerCase" method to convert uppercase characters to lowercase.

  • If your program is carrying out repeated modification operations on strings, you can optionally use the StringBuilder class.

Related Searches:

References

Resources

  • Photo Credit Thinkstock Images/Comstock/Getty Images

Comments

Related Ads

Featured