Priyanshu Priyanshu - 1 year ago 76
Java Question

How can String transformed to only lower case

Arrange string in alphabetical order. I want the string transformed so that uppercase letters are converted to lowercase and other characters are removed the result shouild be sorted ascending.

public static void main(String[] args) {
String text = "She sells sea shells";
String p1 = "aeeeehhllllssssss"; // desired result

char[] charArray = text.toCharArray();
String sortedString = new String(charArray);
System.out.println( sortedString.equals( p1 ));

Answer Source

Replace the line

char[] charArray = text.toCharArray();


char[] charArray = text.toLowerCase().replaceAll( "[^\\p{Lower}]","" ).toCharArray();

This uses a regular expression:

Runnable example:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download