Is there a simple way that I can sort characters in a string in alphabetical order

后端 未结 4 923
囚心锁ツ
囚心锁ツ 2020-12-04 21:12

I have strings like this:

var a = \"ABCFE\";

Is there a simple way that I can sort this string into:

ABCEF
<
相关标签:
4条回答
  • 2020-12-04 21:41
    new string (str.OrderBy(c => c).ToArray())
    
    0 讨论(0)
  • 2020-12-04 21:52

    You can use LINQ:

    String.Concat(str.OrderBy(c => c))
    

    If you want to remove duplicates, add .Distinct().

    0 讨论(0)
  • 2020-12-04 21:54

    Yes; copy the string to a char array, sort the char array, then copy that back into a string.

    static string SortString(string input)
    {
        char[] characters = input.ToArray();
        Array.Sort(characters);
        return new string(characters);
    }
    
    0 讨论(0)
  • 2020-12-04 21:54

    You can use this

    string x = "ABCGH"
    
    char[] charX = x.ToCharArray();
    
    Array.Sort(charX);
    

    This will sort your string.

    0 讨论(0)
提交回复
热议问题