开发者

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

I have strings like this:

var a = "ABCFE";

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

ABCEF
开发者_StackOverflow社区

Thanks


You can use LINQ:

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

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


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);
}


new string (str.OrderBy(c => c).ToArray())


You can use this

string x = "ABCGH"

char[] charX = x.ToCharArray();

Array.Sort(charX);

This will sort your string.


It is another.You can use SortedSet:

var letters = new SortedSet<char> ("ABCFE");
foreach (char c in letters) Console.Write (c); // ABCEF


var sortedString1 = string.Join("", str1.OrderBy(x => x).ToArray());

This will return a sorted string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜