Priyanshu Priyanshu - 21 days ago 7
Java Question

Working with String in Java

Arrange string in alphabetical order. Can anybody please help me on this? Thanks in advance.

public class zI
{

public <T> String(T text)
{
char[] charArray = text.toCharArray();
Arrays.sort(charArray);
String sortedString = new String(charArray);
return sortedString;
}

Answer

Replace the line

char[] charArray = text.toCharArray();

by

char[] charArray = text.replace( " ","" ).toLowerCase().toCharArray();

You could also use regex:

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