Advanced Case Handling
Camel Case Conversion
public class CamelCaseConverter {
public static String toCamelCase(String input) {
if (input == null || input.isEmpty()) {
return input;
}
StringBuilder result = new StringBuilder();
boolean capitalizeNext = false;
for (char c : input.toCharArray()) {
if (Character.isWhitespace(c) || c == '_') {
capitalizeNext = true;
} else if (capitalizeNext) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}
public static void main(String[] args) {
String text = "hello world of java programming";
System.out.println("Camel Case: " + toCamelCase(text));
}
}
Case Conversion Strategies
Conversion Type |
Description |
Example Input |
Result |
Camel Case |
First word lowercase, subsequent words capitalized |
"hello world" |
"helloWorld" |
Snake Case |
Words separated by underscores |
"hello world" |
"hello_world" |
Kebab Case |
Words separated by hyphens |
"hello world" |
"hello-world" |
Advanced Unicode Case Handling
import java.text.Normalizer;
public class UnicodeConverter {
public static String normalizeCase(String input) {
// Normalize Unicode characters
String normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
// Remove non-ASCII characters
return normalized.replaceAll("[^\\p{ASCII}]", "");
}
public static void main(String[] args) {
String unicodeText = "Cafรฉ รฉlรจve";
System.out.println("Normalized: " + normalizeCase(unicodeText));
}
}
Case Conversion Flow
graph TD
A[Input String] --> B{Conversion Type}
B --> |Camel Case| C[Capitalize Words]
B --> |Snake Case| D[Add Underscores]
B --> |Kebab Case| E[Add Hyphens]
B --> |Unicode Normalization| F[Remove Non-ASCII]
public class OptimizedCaseConverter {
public static String efficientConversion(String input) {
if (input == null) return null;
char[] chars = input.toCharArray();
boolean capitalizeNext = true;
for (int i = 0; i < chars.length; i++) {
if (Character.isWhitespace(chars[i])) {
capitalizeNext = true;
} else if (capitalizeNext) {
chars[i] = Character.toUpperCase(chars[i]);
capitalizeNext = false;
} else {
chars[i] = Character.toLowerCase(chars[i]);
}
}
return new String(chars);
}
public static void main(String[] args) {
String text = "HELLO world OF java";
System.out.println("Optimized Conversion: " + efficientConversion(text));
}
}
Advanced Considerations
Key Challenges in Case Conversion
- Handling special characters
- Maintaining Unicode integrity
- Performance optimization
- Locale-specific transformations
Recommended Practices
- Use built-in Java methods when possible
- Implement custom logic for complex scenarios
- Consider performance implications
- Test with various input types
Error Handling and Edge Cases
public class RobustCaseConverter {
public static String safeConversion(String input, ConversionType type) {
if (input == null || input.isEmpty()) {
return input;
}
try {
switch (type) {
case UPPERCASE:
return input.toUpperCase();
case LOWERCASE:
return input.toLowerCase();
case TITLE_CASE:
// Custom title case implementation
return convertToTitleCase(input);
default:
throw new IllegalArgumentException("Unsupported conversion type");
}
} catch (Exception e) {
// Logging or default handling
return input;
}
}
enum ConversionType {
UPPERCASE, LOWERCASE, TITLE_CASE
}
}