[英]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
Thanks谢谢
You can use LINQ:您可以使用 LINQ:
String.Concat(str.OrderBy(c => c))
If you want to remove duplicates, add .Distinct()
.如果要删除重复项,请添加
.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:这是另一个。您可以使用 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这将返回一个排序的字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.