通过子字符串获取整数而不是比较 [英] Get integer by substring and than compare

查看:56
本文介绍了通过子字符串获取整数而不是比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个场景,我有特定格式的字符串。



和ABC00001一样,这些都是增加的。比如ABC0001,ABC0002和最高ABC00000010



增加零的平均数量。

现在好的情况是我想要一个我会通过的功能


$ b $ 00 00001和00000010作为参数和查询应该给我所有那些在这个范围之间有任何字符串的记录。





Hi All,
I have a scenario where I have few strings in specific format.

Like ABC00001 and these are made incremented. like ABC0001, ABC0002 and upto ABC00000010

Mean number of zeroes incremented.
Well now situation is that I want a functionality where i would pass

00001 and 00000010 as parameters and query should give me all those records which have any of the string between this range at the end.


f => f.RegNo.Substring(8).CompareTo(param1) >= 0 && f.RegNo.Substring(8).CompareTo(param2







我试过这个,但确实如此在这种情况下不起作用,相反,如果两个参数长度相同,它就可以工作。




I have tried this but it does not work in this scenario, rather it works if both param length is same.

推荐答案

创建你的比较器:

Create your of comparer:
class StockSeriesComparer : Comparer<string>
{
	public override int Compare(string x, string y)
	{
		if(x == null && y == null) return 0;
		if(x == null) return -1;
		if(y == null) return 1;
		
		if(x.Length < y.Length) return -1;
		if(x.Length > y.Length) return 1;
		
		return x.CompareTo(y);
	}
}



然后你可以在比较中使用它:


Then you can use it in your comparison like this:

f => comparer.Compare(f, lowerBound) >= 0 && comparer.Compare(f, upperBound) <= 0;



即使没有首先删除前缀,这也会有效,假设它们都是相同的。


This will work even without stripping the prefixes first, assuming they are all the same.


这篇关于通过子字符串获取整数而不是比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆