如何比较字符串小于或大于其他字符串? [英] how to compare string like less than or greater than other string?
本文介绍了如何比较字符串小于或大于其他字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我正在开发一个项目,即我将新的字母数字代码分配给选定的字符串。
即字符串是A000到A010
hello ,
I am develpoing one project , in that im assigning the New alpha numeric code to the selected strings.
i.e strings are A000 to A010
string s1 = A000 and String s2= A010;
here String s3 is new string is retrived between s1 ande s2
here i need to do
if (s1 <= s3 && s3>=s2);
{
here i want to insert Alphanumeric code from s1 to s2;
}
适用或任何新建议。
感谢你
is it applicable or any new suggestions.
Thanking you
推荐答案
你可能意味着关注!
You probably meant following!
List<string> list = new List<string>();
for (int index=0; index<=10; index++)
{
List.Add(string.format("A{0:D3}", index);
}
</string></string>
如果您的要求是使用字母数字值以正确的顺序添加项目,请检查以下示例代码
if your requirement is adding items in correct order by using alphanumeric values, check below sample code
void Main()
{
SortedList<string,student> list = new SortedList<string,student>(new MyComparer());
list.Add("A000",new Student(){Name ="A"});
list.Add("A010",new Student(){Name ="F"});
list.Add("A005",new Student(){Name ="G"});
//list.Dump();
}
class MyComparer : IComparer<string>
{
public int Compare(string a, string b)
{
var x = int.Parse(Regex.Replace(a, "[^0-9]", ""));
var y = int.Parse(Regex.Replace(b, "[^0-9]", ""));
if (x != y) return x - y;
return -1 * string.Compare(a, b);
}
}
class Student
{
public string Name{get;set;}
}
</string>
你可以使用SortedList,它会根据给定的比较器对项目进行排序,这里我为您的字母数字值添加了自定义比较器。
you can use SortedList and it will sort item based on given comparer, here i have added custom comparer for your alphanumeric values.
.NET String对象有一个内置的比较方法...使用它!
http://msdn.microsoft.com/en-us/library/System.String 。比较(v = vs.110).aspx [ ^ ]
.NET String object has a built in comparison method...Use it!
http://msdn.microsoft.com/en-us/library/System.String.Compare(v=vs.110).aspx[^]
这篇关于如何比较字符串小于或大于其他字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文