Technology Programming

How to Make the First Letter in a String a Capital in Java

    • 1). Prepare the text string within your Java program. The following example code demonstrates a string variable being instantiated:

      String someText = "here is some text";

      If you already have a string variable, you can use that one. The text string in question can contain any different characters you like, including punctuation symbols and numbers, as long as the first character is a letter.

    • 2). Isolate the first character in your string. To convert the first character in your string to uppercase, you first need to provide Java with a reference to it. Enter the following code:

      someText.substring(0, 1)

      This code excerpt uses the substring method to isolate the first character in the string by providing the start and end positions of the required string section. This is just part of the final code line that will capitalize the first letter in your string. The process requires a number of distinct operations working in conjunction with one another. By working through these in turn, you will have a clearer understanding of the technique you are using.

    • 3). Convert the first character in your string to uppercase. The Java "toUpperCase" method works only on strings, not on individual characters, so you must perform the operation on the character as a string. Add to your line of code as follows:

      someText.substring(0, 1).toUpperCase()

      The code is calling the "toUpperCase" method on the first letter in the string, represented as a substring.

    • 4). Copy the results of your capitalization into a variable. So far, your code has converted the first letter in the string to uppercase, but it has carried out this operation on a separate substring rather than on the original string. To perform the conversion operation on your original string variable, you need to assign the result of the operation to it as follows:

      someText=someText.substring(0, 1).toUpperCase()

      This code overwrites the value in the original variable with the results of your capitalization operation. However, at the moment, the part of the string following the first letter has been excluded.

    • 5). Complete your string capitalization by adding the rest of the string to the variable. Enter the final code statement as follows:

      someText=someText.substring(0, 1).toUpperCase()+someText.substring(1);

      This takes the original string, capitalizes the first character, and then joins this with the rest of the string, replacing the original variable value with the result of the operation. You can test your new string by writing it to the Java output console as follows:

      //output "Here is some text"

      System.out.println(someText);

Related posts "Technology : Programming"

Differences Between Byte Array Vs. String

Programming

Web Design Company UK/

Programming

Develop A Quality Plan With These Self Help Tips

Programming

TMediaPlayer: What track am I on?

Programming

How You Can Prove Your Expertise

Programming

Furniture Advice And Tips And Also Hardwearing. Property Searching Wonderful

Programming

Web Design Fort Lauderdale Business

Programming

Hire Web Designer and Diminish Development Expenditure

Programming

Web Masters.

Programming

Leave a Comment