Java have a lot of method for string operations. We can sort a string by convert in to array of characters , now we will sort array after that we will make a new string from sorted array.Now our new string is in sorted order of characters.
package com.javaproficiency;
import java.util.Arrays;
public class StringSort {
public static void main(String[] args) {
String str = "xyabds";
char [] arr = str.toCharArray();
Arrays.sort(arr);
String sortedStr = new String(arr);
System.out.println(" sorted string = "+sortedStr);
}
}
Output:
sorted string = abdsxy
package com.javaproficiency;
import java.util.Arrays;
public class StringSort {
public static void main(String[] args) {
String str = "xyabds";
char [] arr = str.toCharArray();
Arrays.sort(arr);
String sortedStr = new String(arr);
System.out.println(" sorted string = "+sortedStr);
}
}
Output:
sorted string = abdsxy
No comments:
Post a Comment