🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Character.toLowerCase() method in Java is used to convert a specified character to its lowercase equivalent.
Table of Contents
- Introduction
toLowerCase()Method Syntax- Examples
- Converting Single Characters
- Handling Non-Alphabetic Characters
- Converting Unicode Code Points
- Real-World Use Case
- Conclusion
Introduction
The Character.toLowerCase() method is a static method in the Character class in Java. It converts a given character to its lowercase equivalent based on the Unicode standard. This method is useful for standardizing text to lowercase for comparison, storage, or processing.
toLowerCase()() Method Syntax
The Character.toLowerCase() method has two overloaded versions:
toLowerCase(char ch)
public static char toLowerCase(char ch)
- ch: The character to be converted to lowercase.
The method returns:
- The lowercase equivalent of the character.
toLowerCase(int codePoint)
public static int toLowerCase(int codePoint)
- codePoint: The Unicode code point to be converted to lowercase.
The method returns:
- The lowercase equivalent of the code point.
Examples
Converting Single Characters
The toLowerCase(char ch) method can be used to convert a single character to its lowercase equivalent.
Example
public class ToLowerCaseExample {
public static void main(String[] args) {
char upperCaseChar = 'A';
char lowerCaseChar = Character.toLowerCase(upperCaseChar);
System.out.println("Lowercase of 'A': " + lowerCaseChar);
}
}
Output:
Lowercase of 'A': a
In this example, the method converts the uppercase letter 'A' to its lowercase equivalent 'a'.
Handling Non-Alphabetic Characters
The toLowerCase() method returns the character unchanged if it is not an uppercase letter.
Example
public class NonAlphabeticCharacterExample {
public static void main(String[] args) {
char digitChar = '1';
char specialChar = '$';
char lowerDigitChar = Character.toLowerCase(digitChar);
char lowerSpecialChar = Character.toLowerCase(specialChar);
System.out.println("Lowercase of '1': " + lowerDigitChar);
System.out.println("Lowercase of '$': " + lowerSpecialChar);
}
}
Output:
Lowercase of '1': 1
Lowercase of '$': $
In this example, the method returns the non-alphabetic characters '1' and '$' unchanged.
Converting Unicode Code Points
The toLowerCase(int codePoint) method can be used to convert a Unicode code point to its lowercase equivalent.
Example
public class ToLowerCaseCodePointExample {
public static void main(String[] args) {
int upperCodePoint = 'B';
int lowerCodePoint = Character.toLowerCase(upperCodePoint);
System.out.println("Lowercase of code point 'B': " + (char) lowerCodePoint);
}
}
Output:
Lowercase of code point 'B': b
Handling Special Unicode Characters
The toLowerCase(int codePoint) method works with special Unicode characters as well.
Example
public class SpecialUnicodeCharacterExample {
public static void main(String[] args) {
int upperGreekSigma = '\u03A3'; // Greek capital letter Sigma
int lowerGreekSigma = Character.toLowerCase(upperGreekSigma);
System.out.println("Lowercase of Greek Sigma: " + (char) lowerGreekSigma);
}
}
Output:
Lowercase of Greek Sigma: σ
In this example, the method converts the Greek capital letter Sigma to its lowercase equivalent.
Real-World Use Case
Converting Text to Lowercase
In a real-world application, you might need to convert an entire string to lowercase before processing it further.
Example
public class ConvertStringToLowerCaseExample {
public static void main(String[] args) {
String text = "Hello World!";
StringBuilder lowerText = new StringBuilder();
for (char ch : text.toCharArray()) {
lowerText.append(Character.toLowerCase(ch));
}
System.out.println("Converted text: " + lowerText.toString());
}
}
Output:
Converted text: hello world!
In this example, the code converts all characters in the input string to lowercase.
Conclusion
The Character.toLowerCase() method in Java is a simple and effective way to convert characters to their lowercase equivalents. By understanding how to use this method and its overloaded versions, you can efficiently handle text processing tasks that involve converting characters to lowercase in your Java applications. Whether you are converting individual characters, handling Unicode code points, or processing entire strings, the toLowerCase() method provides a reliable solution for these tasks.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment