Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String toUpperCase()

Java String toUpperCase() transforms a string by replacing all lowercase characters to uppercase while leaving any characters already in uppercase.

Lokesh Gupta

October 10, 2023

Java String class
Java String
Java String

The toUpperCase() method is a member of the java.lang.String class. The toUpperCase() method transforms a string by replacing all lowercase characters with their corresponding uppercase counterparts, while leaving any characters that are already in uppercase unaffected.

See Also: String toLowerCase() Method

1. String.toUpperCase() Method

The toUpperCase() method is an overloaded method. It takes an optional argument of type Locale.

  • When Locale information is passed, the method returns a new String after converting all the alphabetic characters to uppercase according to the specified Locale rules.
  • When Locale information is NOT passed, the default locale rules are applied.
public String toUpperCase()  //Uses default Locale rules

public String toUpperCase(Locale locale)  //Optional Locale information

Let us start with a very simple example to understand the usage of toUpperCase() method.

String original = "Hello, World!";

String upperCase = original.toUpperCase();
//String upperCase = original.toUpperCase(Locale.US);   //Optional Locale information

System.out.println(upperCase);     // Output: HELLO, WORLD!

As demonstrated above, the toUpperCase() method converts all letters in the string to uppercase, making it a handy tool for various string manipulation tasks.

The toUppercase() method is equal to calling the toUpperCase(Locale.getDefault()) method that uses the current Locale rules.

2. String.toUpperCase() with Custom Locale

The following Java program converts a string to uppercase using the Locale.US. When you run this code, it will convert the Greek string “Γειά σου Κόσμε” to uppercase using the rules of the United States locale.

String uppercaseString = "Γειά σου Κόσμε".toUpperCase(Locale.US);

System.out.println(uppercaseString);

The output will be:

ΓΕΙΆ ΣΟΥ ΚΌΣΜΕ

3. Usages of toUpperCase() Method

We can use the upper case strings in many real-world problems during data processing.

For example, when we need to sort a list of strings case-insensitively, we can use toUpperCase() in the sorting Comparator. This ensures that the sorting is done without considering the letter case.

List<String> names = Arrays.asList("alice", "Bob", "charlie", "dave");

Collections.sort(names, (s1, s2) -> s1.toUpperCase().compareTo(s2.toUpperCase()));

System.out.println(names); // Output: [alice, Bob, charlie, dave]

Similarly, we can use toUpperCase() to perform case-insensitive string searches. We can convert both the search term and the target string to uppercase. Using toUpperCase() ensures that the search is not case-sensitive.

String text = "Java is a powerful programming language";
String searchTerm = "java";

if (text.toUpperCase().contains(searchTerm.toUpperCase())) {
    System.out.println("Match found!");
} else {
    System.out.println("Match not found.");
}

4. Length of the Original and Uppercase Strings can be Unequal

Note that the length of the original string and upper string may be different due to a few locale-specific characters.

In this example, we have a string "straße", which means “street” in German. When we convert it to uppercase using the German locale (Locale.GERMAN), the ‘ß’ character transforms into ‘SS’.

String originalText = "straße"; // German word for "street"
String uppercaseText = originalText.toUpperCase(Locale.GERMAN); // Using the German locale

System.out.println("Original Text: " + originalText);
System.out.println("Uppercase Text: " + uppercaseText);
System.out.println("Original Length: " + originalText.length());
System.out.println("Uppercase Length: " + uppercaseText.length());

The program output:

Original Text: straße
Uppercase Text: STRASSE

Original Length: 6
Uppercase Length: 7

So, when working with the toLowerCase() method in Java and considering locale-specific characters, it’s essential to be aware that the length of the resulting string may not always be the same.

Happy Learning !!

References: String Java Doc

Source Code on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

Table of Contents

  • 1. String.toUpperCase() Method
  • 2. String.toUpperCase() with Custom Locale
  • 3. Usages of toUpperCase() Method
  • 4. Length of the Original and Uppercase Strings can be Unequal
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String split() : Splitting by One or Multiple Delimiters

Next

Java String toLowerCase()

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap