Home> Java> javaTutorial> body text

How to convert string to uppercase using toUpperCase() function of String class in Java

WBOY
Release: 2023-07-26 16:01:14
Original
2182 people have browsed it

How does Java use the toUpperCase() function of the String class to convert a string to uppercase

In Java, the String class is a very commonly used class that provides many methods for processing strings. . One very useful method is toUpperCase(), which converts a string to uppercase.

The use of the toUpperCase() method is very simple, just call this method. The following is a sample code that shows how to use the toUpperCase() method to convert a string to uppercase:

public class Main { public static void main(String[] args) { String str = "hello, world!"; String upperCaseStr = str.toUpperCase(); System.out.println(upperCaseStr); } }
Copy after login

Run the above code, the output is:

HELLO, WORLD!
Copy after login
Copy after login

As shown above, use toUpperCase () method can quickly convert strings to uppercase. Not only that, the toUpperCase() method can also handle special characters and non-English letters.

public class Main { public static void main(String[] args) { String str = "你好,world! 123"; String upperCaseStr = str.toUpperCase(); System.out.println(upperCaseStr); } }
Copy after login

Run the above code, the output result is:

你好,WORLD! 123
Copy after login

As you can see, the toUpperCase() method also converts Chinese characters and special characters into uppercase form.

The toUpperCase() method can be combined with other String class methods to achieve more complex string processing operations. The following is an example that demonstrates how to convert the first letter of each word in a string to uppercase:

public class Main { public static void main(String[] args) { String str = "hello, world!"; String[] words = str.split(" "); StringBuilder result = new StringBuilder(); for (String word : words) { String firstLetter = word.substring(0, 1).toUpperCase(); String restLetters = word.substring(1); String upperCaseWord = firstLetter + restLetters; result.append(upperCaseWord).append(" "); } System.out.println(result.toString().trim()); } }
Copy after login

Running the above code, the output is:

HELLO, WORLD!
Copy after login
Copy after login

As shown above, By combining the toUpperCase() method with other string processing methods (such as split(), substring()), we can achieve more flexible and complex string processing operations.

When using the toUpperCase() method, it should be noted that the toUpperCase() method returns a new string object rather than modifying the original string object. Therefore, when you need to use the converted uppercase string, it is recommended to assign the conversion result to a new string variable.

In summary, the String class in Java provides a wealth of methods for processing strings. Among them, the toUpperCase() method is a powerful method that can quickly convert a string into uppercase form. By flexibly using the toUpperCase() method and other string processing methods, we can implement more complex and flexible string processing operations. This method is very useful both in daily development and algorithmic problem solving.

The above is the detailed content of How to convert string to uppercase using toUpperCase() function of String class in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!