The intent of this page is to collection the best program and help programmers to find good blog posts to read. Some of these blogs may not be written by Java developers, but Java developers should find it useful or interesting. Reading those blogs should be fun and often bring some fresh ideas

Sunday, December 13, 2020

String Operation

String Operation Using collection Framework

Sorting Order

 public static void sortAccending(String s) {

char[] arr = s.toCharArray();

Arrays.sort(arr);

System.out.print(arr);

}

Sorting String Descending Order

public static void SortingDesendingOrder(String s) {

char[] arr = s.toCharArray();

Character[] a = new Character[arr.length];

for (int i = 0; i < a.length; i++)

a[i] = arr[i];

Arrays.sort(a, (o1, o2) -> { return -1;});

for (int i = 0; i < a.length; i++)

arr[i] = a[i];

System.out.print(arr);

}

RemoveDublicate Inside String 

public static void RemoveDublicate(String s) {

Set<Character> s1 = new HashSet<>();

for(int i=0; i<s.length(); i++)

s1.add(s.charAt(i));

String temp = "";

                for(char ch: s1) {

        temp = temp+ch+"";

                 }

                 System.out.println(temp);

}

Other Logic

    /* removeDublicate

     * LinkedHashSet<Charactor> s = new LinkedHashSet<Charactor>();

       int n = str.length();

       for(int i=0; i<n; i++)

           s.add(str.charAt(i));

        System.out.println(s);

        return str;

     */


No comments:

Post a Comment

If you have any doubt . Please let me know

String Operation

String Operation Using collection Framework Sorting Order  public static void sortAccending(String s) { char[] arr = s.toCharArray(); ...

Adbox