如何在asp.net中排序字符串 [英] how t sort string in asp.net
问题描述
我有这样的字符串a12,a11,a20,a45
现在我想将此字符串排序为以下格式
a11,a12,a20,a45 how我将在asp.net中执行此操作
取决于 - 如果字符串是字面意思:
< span class =code-keyword> string s = a12,a11,a20,a45;
然后,要做的第一件事是分离要排序的项目。 String.Split会这样做:
string s = a12,a11,a20,a45;
string [] parts = s.Split(' ,');
然后你可以对零件进行排序:
string s = a12,a11,a20,a45跨度>;
string [] parts = s.Split(' 跨度>);
Array.Sort(部分);
最后,重建字符串:
< span class =code-keyword> string s = a12,a11,a20,a45< /跨度>;
string [] parts = s.Split(' 跨度>);
Array.Sort(parts);
s = string .Join( , ,parts);
拆分字符串
以创建字符串数组
:
string [] strArr = yourStr.Split( ' ,');
然后排序数组并重新创建所需的字符串:
Array.Sort(strArr);
string .Join( ,,strArr);
现在你有一个已排序的字符串格式化。
一个不错的选择数组排序,使用LINQ可以是:
var sort = 来自 s strArr
orderby s
选择 s;
干杯,
c
参考以下文章:
1)使用ASP.NET中的数组和字符串来排序方法
2)如何在asp.net c#<中排序一个数组/ A>
I have string like this a12,a11,a20,a45
now i want to sort this string into the following format
a11,a12,a20,a45 how i will do this in asp.net
Depends - if the string is literally:
string s = "a12,a11,a20,a45";Then the first thing to do is separate the items you want to sort. String.Split will do that:
string s = "a12,a11,a20,a45"; string[] parts = s.Split(',');
You can then sort the parts:
string s = "a12,a11,a20,a45"; string[] parts = s.Split(','); Array.Sort(parts);
And finally, rebuild the string:
string s = "a12,a11,a20,a45"; string[] parts = s.Split(','); Array.Sort(parts); s = string.Join(",", parts);
Split yourstring
to create astring array
:
string[] strArr = yourStr.Split(',');
then sort the array and recreate the desired string:
Array.Sort(strArr); string.Join(",", strArr);
Now you have a sorted string formatted.
An nice alternative to the array sort, using LINQ can be:
var sort = from s in strArr orderby s select s;
Cheers,
c
Hi,
Ref this following articles :
1) Use Array and String in ASP.NET to Sort Method
2) How to sort an array in asp.net c#
这篇关于如何在asp.net中排序字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!