Converts a String to upper case as per String#toUpperCase() - Java java.lang

Java examples for java.lang:String Case

Description

Converts a String to upper case as per String#toUpperCase()

Demo Code

//package com.java2s;

import java.util.Locale;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(upperCase(str));
    }// w w w  .j  av  a2s.  c o m

    /**
     * <p>Converts a String to upper case as per {@link String#toUpperCase()}.</p>
     *
     * <p>A <code>null</code> input String returns <code>null</code>.</p>
     *
     * <pre>
     * upperCase(null)  = null
     * upperCase("")    = ""
     * upperCase("aBc") = "ABC"
     * </pre>
     *
     * <p><strong>Note:</strong> As described in the documentation for {@link String#toUpperCase()},
     * the result of this method is affected by the current locale.
     * For platform-independent case transformations, the method {@link #lowerCase(String, Locale)}
     * should be used with a specific locale (e.g. {@link Locale#ENGLISH}).</p>
     *
     * @param str  the String to upper case, may be null
     * @return the upper cased String, <code>null</code> if null String input
     */
    public static String upperCase(String str) {
        if (str == null) {
            return null;
        }
        return str.toUpperCase();
    }

    /**
     * <p>Converts a String to upper case as per {@link String#toUpperCase(Locale)}.</p>
     *
     * <p>A <code>null</code> input String returns <code>null</code>.</p>
     *
     * <pre>
     * upperCase(null, Locale.ENGLISH)  = null
     * upperCase("", Locale.ENGLISH)    = ""
     * upperCase("aBc", Locale.ENGLISH) = "ABC"
     * </pre>
     *
     * @param str  the String to upper case, may be null
     * @param locale  the locale that defines the case transformation rules, must not be null
     * @return the upper cased String, <code>null</code> if null String input
     * @since 3.0
     */
    public static String upperCase(String str, Locale locale) {
        if (str == null) {
            return null;
        }
        return str.toUpperCase(locale);
    }
}

Related Tutorials