有没有一种简单的方法可以用字母顺序对字符串中的字符进行排序

我有这样的线:

var a = "ABCFE";

有没有一种简单的方法可以把这个字符串分类为:

ABCEF

谢谢

155658 次浏览

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