简体   繁体   English

在C#中优化字符串比较

[英]Optimize string comparison in C#

I have the following code on an asp.net button click 我在asp.net按钮上单击以下代码

if(str == ipaddr1 || ipaddr2 || ipaddr3 || ipaddr4 || ipaddr5 || ipaddr6 || ipaddr7)
// do this
else
//cancel click event

How can I optimize this piece of code? 我如何优化这段代码?

Update: Apologies everyone! 更新:道歉大家! I did not mean to compare it with the literal string ipaddr. 我并不是想将它与文字字符串ipaddr进行比较。 I mean to compare it to the value ipaddr1, ipaddr2 holds and so on 我的意思是将其与ipaddr1,ipaddr2持有的值进行比较,依此类推

replace with: 用。。。来代替:

Regex.IsMatch(str, "^ipaddr[1-7]$")

Optimised for readability not necessarily performance. 针对可读性进行了优化(不一定是性能)。

HashSet<T> is the best container to check containing: HashSet<T>是检查以下内容的最佳容器:

var ips = new HashSet<string> { "ip1", "ip2", "ip3", "ip4", "ip5" };
if (ips.Contains(input))
{
    // do stuff
}

For any type: 对于任何类型:

var ips = new HashSet<IPHostEntry> { ip1, ip2, ip3, ip4, ip5 };
if (ips.Contains(input))
{
    // do stuff
}

Was: 是:

if(str = qry.StartsWith("23.55") || str = qry.StartsWith("xuz") || str = qry.StartsWith("i3") || str = qry.StartsWith("i444") || str = qry.StartsWith("ki5") || str = qry.StartsWith("65fr6")) // do this else // do this

Become: 成为:

var arr = new[] { "23.55", "xuz", "i3",  "i444", "ki5", "65fr6") };
if (arr.Any(str => input.StartsWith(str, StringComparison.Ordinal))
{
    // do stuff
}

StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase are very important for performance. StringComparison.OrdinalStringComparison.OrdinalIgnoreCase对性能非常重要。

I would do something like: 我会做类似的事情:

str.Length == 7 && str.StartsWith("ipaddr") && str[6] > '0' && str[6] < '8'

Edit : 编辑

After your update, I would do something like: 更新后,我将执行以下操作:

string[] validStrings = { ipaddr1, ipaddr2, ... };
bool isStrValid = validStrings.Contains(str);

For better performance, consider using a HashSet<string> instead of an array, especially if the list of valid strings doesn't change. 为了获得更好的性能,请考虑使用HashSet<string>而不是数组,尤其是在有效字符串列表不变的情况下。

What about 关于什么

if(str.Substring(0,6) == "ipaddr" && str[6] >= '1' && str[6] <= '7')

For your information, your original code does not even compile. 仅供参考,您的原始代码甚至没有编译。 This 这个

if(str == "ipaddr1" || "ipaddr2" || "ipaddr3" || "ipaddr4" || "ipaddr5" || "ipaddr6" || "ipaddr7")

Needs to be replaced with this to compile 需要替换为此进行编译

if(str == "ipaddr1" || str == "ipaddr2" || str == "ipaddr3" || str == "ipaddr4" || str == "ipaddr5" || str == "ipaddr6" || str == "ipaddr7")

So the original code is actually even more tedious than you thought. 因此,原始代码实际上比您想象的还要乏味。

UPDATE 更新

According to your updated question, the best option is to put your string variables into a List<string> called, for example ipaddr . 根据您更新的问题,最好的选择是将字符串变量放入一个名为List<string>List<string> ,例如ipaddr Then to see if the string str is included, simply do this: 然后查看字符串str是否包含在内,只需执行以下操作:

if( ipaddr.Contains( str ) )
{
   //contained in the list
}

I would do a 我会做一个

List<string> variables = new List<string> { "ip1","ip2","ip3","ip4","ip5" };

if (variables.Contains(inputstring))
  ...

Both more readable, and more performant would be: 更具可读性和更高性能的将是:

switch(str)
{
case "ipaddr1": 
case "ipaddr2":
case "ipaddr3":
case "ipaddr4":
case "ipaddr5":
case "ipaddr6":
case "ipaddr7":
    //do something
    break;
default:
    //do something else
    break;
}

(although, admittedly massively verbose...) (尽管,非常冗长...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM