Simple string concatenation manipulation in java

I want to get the following output:

Hi Steve Andrews!

These are my variables:

a = "steve";
b = "Andrew"

I tried this:

System.out.print("Hello " + a + " " + b + "s");

I do not know where to put .toUpper()for steve. smust be uppercase. How to do it?

+5
source share
4 answers

Finally, I tried to do this without stringutils. But anyway, thanks to everyone who helped :)

public class Hello
    {
      public static void main(String[] args){
        String a = "steve";
        String b = "Andrew";
        String firstletter = a.substring(0,1);
        String remainder = a.substring(1);
        String capitalized = firstletter.toUpperCase() + remainder.toLowerCase();

        System.out.print("Hello " + capitalized + " " + b + "s" );

    }
}
0
source

use StringUtils.capitalize(a),

"Hello " + StringUtils.capitalize(a) + " " + b + "s"

A header line that changes the first letter to a title according to the .toTitleCase (char) character. No other letters are changed.

+7
source

StringUtils.capitalize(str), :

public static String capitalize(String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }
    return new StringBuffer(strLen)
        .append(Character.toTitleCase(str.charAt(0)))
        .append(str.substring(1))
        .toString();
}
+3

:

public static String capitalize(String str) {
    str = (char) (str.charAt(0) - 32) + str.substring(1);
    return str;
}

code>
Although it should be noted that this method assumes that the first character in str is indeed a lowercase letter.

0
source

All Articles