Character Manipulation Techniques
Case Conversion Methods
public class CaseManipulation {
public static void main(String[] args) {
Character ch1 = 'a';
Character ch2 = 'Z';
// Uppercase conversion
System.out.println("To Uppercase: " + Character.toUpperCase(ch1));
// Lowercase conversion
System.out.println("To Lowercase: " + Character.toLowerCase(ch2));
}
}
Character Type Checking
Comprehensive Type Validation
public class CharacterTypeCheck {
public static void main(String[] args) {
Character[] chars = {'A', '5', ' ', '@'};
for (Character ch : chars) {
System.out.println("Character: " + ch);
System.out.println("Is Letter: " + Character.isLetter(ch));
System.out.println("Is Digit: " + Character.isDigit(ch));
System.out.println("Is Whitespace: " + Character.isWhitespace(ch));
System.out.println("Is Special Character: " + !Character.isLetterOrDigit(ch));
System.out.println("---");
}
}
}
Character Type Checking Methods
Method |
Description |
Example |
isLetter() |
Checks if character is a letter |
Character.isLetter('A') |
isDigit() |
Checks if character is a digit |
Character.isDigit('5') |
isWhitespace() |
Checks if character is whitespace |
Character.isWhitespace(' ') |
isLetterOrDigit() |
Checks if character is letter or digit |
Character.isLetterOrDigit('B') |
Character Comparison Techniques
Advanced Comparison Methods
public class CharacterComparison {
public static void main(String[] args) {
Character ch1 = 'A';
Character ch2 = 'a';
// Comparison ignoring case
System.out.println("Equals Ignore Case: " +
Character.toLowerCase(ch1) == Character.toLowerCase(ch2));
// Numeric comparison
System.out.println("Numeric Compare: " +
Character.compare(ch1, ch2));
}
}
Character Manipulation Flow
graph TD
A[Character Manipulation]
A --> B[Case Conversion]
A --> C[Type Checking]
A --> D[Comparison]
A --> E[Parsing]
Character Parsing and Conversion
public class CharacterParsing {
public static void main(String[] args) {
// Digit to numeric value
Character digitChar = '7';
int numericValue = Character.getNumericValue(digitChar);
System.out.println("Numeric Value: " + numericValue);
// Unicode manipulation
Character unicodeChar = '\u00A9';
System.out.println("Unicode Character: " + unicodeChar);
}
}
Unicode and Advanced Manipulation
Unicode Character Handling
public class UnicodeManipulation {
public static void main(String[] args) {
// Unicode range checking
Character ch = 'Ï';
System.out.println("Unicode Value: " + (int)ch);
System.out.println("Is Unicode Letter: " + Character.isLetter(ch));
}
}
LabEx Insight
At LabEx, we recommend mastering these character manipulation techniques to write more robust and flexible Java code.
- Use primitive
char
for simple operations
- Leverage
Character
class methods for complex manipulations
- Be mindful of method call overhead in performance-critical sections