
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Lowercase and Uppercase in Java
In Java, converting a string to lowercase or uppercase is a common operation used in text processing, formatting, and case normalization. The String class provides built-in methods to achieve this with ease. This article explores how to use these methods with practical examples, demonstrating step-by-step how to convert strings between uppercase and lowercase.
Key Methods
- toLowerCase(): Converts all characters in a string to lowercase.
- toUpperCase(): Converts all characters in a string to uppercase.
These methods are part of the java.lang.String class and work seamlessly for any string input.
Using toLowerCase() and toUpperCase()
In this approach, we use the methods toLowerCase() and toUpperCase() to directly convert the case of a string.
Convert to Lowercase ?
- The toLowerCase() method processes each character in the string and converts uppercase letters (A-Z) to their lowercase counterparts (a-z).
- Non-alphabetic characters remain unchanged.
str1 = "TUTORIALS POINT";System.out.println("string value = " + str1.toLowerCase());
Convert to Uppercase ?
- The toUpperCase() method converts lowercase letters (a-z) to uppercase (A-Z).
- Again, non-alphabetic characters remain unaffected.
str2 = "www.tutorialspoint.com"; System.out.println("string value = " + str2.toUpperCase());
Example
Below is an example of converting a string to lowercase and uppercase ?
import java.lang.*; public class StringDemo { public static void main(String[] args) { // converts all upper case letters in to lower case letters String str1 = "SELF LEARNING CENTER"; System.out.println("string value = " + str1.toLowerCase()); str1 = "TUTORIALS POINT"; System.out.println("string value = " + str1.toLowerCase()); // converts all lower case letters in to upper case letters String str2 = "This is TutorialsPoint"; System.out.println("string value = " + str2.toUpperCase()); str2 = "www.tutorialspoint.com"; System.out.println("string value = " + str2.toUpperCase()); } }
Output
string value = self learning center string value = tutorials point string value = THIS IS TUTORIALSPOINT string value = WWW.TUTORIALSPOINT.COM
Use Cases
- Case-Insensitive Comparisons: Normalize string cases for comparisons in search engines or text matching.
- User Input Formatting: Format user inputs such as names, email addresses, and titles.
- Text Analysis: Standardize text data for processing and analysis.
Conclusion
Using Java's toLowerCase() and toUpperCase() methods is an efficient way to convert strings to lowercase and uppercase, respectively. This simple yet powerful functionality is essential for text formatting, data normalization, and many other string-related operations in Java.