Which method can be used to return a string in upper case letters in Java

The syntax of the string toUpperCase() method is:

string.toUpperCase()

toUpperCase() Parameters

  • doesn't take any parameters

toUpperCase() Return Value

  • returns a string with all lower case letters converted to upper case

Example: Java toUpperCase()

class Main { public static void main(String[] args) { String str1 = "Learn Java"; String str2 = "Java123"; // convert to upper case letters System.out.println(str1.toUpperCase()); // "LEARN JAVA" System.out.println(str2.toUpperCase()); // "JAVA123" } }

As you can see from the above example, toUpperCase() converts all lower case letters to upper case letters.

toUpperCase() With Locale Parameter

The toUpperCase() method can also take a locale as an argument. This allows you to convert all characters in a string to upper case using the given Locale (such as: Turkish, Lithuanian etc.) rules.

Its syntax is:

string.toUpperCase(Locale locale)

If you do not pass the locale parameter, the default locale, Locale.getDefault(), is used.

To learn more, visit Java toUpperCase() With Locale.

To convert all characters in a string to lower case letters, use the Java String toLowerCase() method.

A complete learning experience in Java and object-oriented programming. With this book, you'll learn the Java language with a unique method that goes beyond how-to manuals and helps you become a great programmer

👉 We may earn a commission when you make a purchase, at no additional cost to you.

In this quick tutorial, you will learn how to capitalize the first letter of a string in Java. Unfortunately, the String class does not provide any method to capitalize strings. However, there are methods available to change the case of the string (uppercase, lowercase, etc.).

The simplest way to capitalize the first letter of a string in Java is by using the String.substring() method:

String str = "hello world!"; String output = str.substring(0, 1).toUpperCase() + str.substring(1); System.out.println(output);

The above example transforms the first letter of string str to uppercase and leaves the rest of the string the same. If the string is null or empty, the above code throws an exception.

However, we can write a functional capitalize() that makes sure the string has at least one character before using the substring() method:

public static String capitalize(String str) { if(str == null || str.isEmpty()) { return str; } return str.substring(0, 1).toUpperCase() + str.substring(1); }

Now just call the capitalize() method whenever you want to make the first letter of a string uppercase:

System.out.println(capitalize("hello world!")); System.out.println(capitalize("heLLo")); System.out.println(capitalize(null));

Notice the 2nd line of the above code snippet, only the first letter is capitalized. If you want to ensure only the first letter is in uppercase and the remaining string is lowered case, you can do the following:

str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase()

The Apache Commons Lang library is another way to capitalize the first letter of a string in Java. If you are using Gradle, add the following dependency to your build.gradle file:

implementation 'org.apache.commons:commons-lang3:3.9'

For Maven, add the dependency below to your pom.xml file:

<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>

The StringUtils class from Commons Lang provides the capitalize() method to change the first letter to uppercase:

System.out.println(StringUtils.capitalize("apache commons")); System.out.println(StringUtils.capitalize("heLLO")); System.out.println(StringUtils.uncapitalize(null));

Apache Commons Lang offers many extra methods that are not included in standard Java libraries (java.lang package). It includes String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization, and System properties.

In this article, we looked at different ways to capitalize the first letter of a string in Java. The simple approach is to use the String class's substring() method. However, if you are already using Apache Commons Lang in your project, just use the StringUtils class to capitalize the first letter of a string.

Since string capitalization is a common task in all programming languages, I have also written a tutorial to do the same in JavaScript. Take a look at this guide.

Read Next: Capitalize the first letter of each word in a string using Java

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

Postingan terbaru

LIHAT SEMUA