5

I want to get the following output:

Hello Steve Andrews!

These are my variables:

a = "steve";
b = "Andrew"

I tried this:

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

I don't know where to put .toUpper() for steve. The s should be in uppercase. How do I do this?

4 Answers 4

7

Use StringUtils.capitalize(a),

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

Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char). No other letters are changed.

Sign up to request clarification or add additional context in comments.

4 Comments

hey what will I import when using stringutils?
You need to add the commons-lang.jar in your classpath and import org.apache.commons.lang.StringUtils
It is now include in Java : // (version 1.7 : 51.0, super bit) public class com.sun.xml.internal.ws.util.StringUtils {
Don't ever use packages starting with com.sun.* or sun.*. They're internal and are no longer available in Java 9.
2

You could use StringUtils.capitalize(str), or if you want to do it by yourself:

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();
}

2 Comments

For a utility method, I'd recommend StringBuilder avoiding unnecessary synchronization overhead.
+1 for using Character, if you are not in Java 7, otherwise use StringUtils like Jigar Joshi show.
0

You could also try using this method:

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


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

3 Comments

... and not just any lowercase letter. It basically works only in the ASCII range. Given the existence of Charecter.toUpperCase this approach is rather misguided.
I just figured that since it's such a simple case, this method should be sufficient and gives a little insight as to how a built-in method such as Character.toUpperCase might work at a lower level.
Perhaps... (btw downvote not mine) although it would be much more instructive if we were in the year 1992 :) Today it's more harm than good. Character.toUpperCase doesn't do any such simple thing; it consults some internal lookup tables---it's a general mechanism that works the same way for all Unicode.
0

Finally, I tried to do it without stringutils.. But anyways, thanks to all 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" );

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.