优化C#中的字符串比较

发布于 2024-10-21 06:55:28 字数 296 浏览 4 评论 0原文

我在 asp.net 按钮上有以下代码,单击

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

如何优化这段代码?

更新:向大家道歉!我并不是想将它与文字字符串 ipaddr 进行比较。我的意思是将其与 ipaddr1、ipaddr2 所持有的值进行比较,依此类推

I have the following code on an asp.net button click

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. I mean to compare it to the value ipaddr1, ipaddr2 holds and so on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故事和酒 2024-10-28 06:55:28

替换为:

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

针对可读性进行优化,不一定是针对性能进行优化。

replace with:

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

Optimised for readability not necessarily performance.

惜醉颜 2024-10-28 06:55:28

HashSet 是检查包含的最佳容器:

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

对于任何类型:

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

变成:

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

StringComparison.OrdinalStringComparison.OrdinalIgnoreCase 非常对性能很重要。

HashSet<T> is the best container to check containing:

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.

意中人 2024-10-28 06:55:28

供您参考

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

,您的原始代码甚至无法编译。 所以

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

需要替换成this才能编译

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

原来的代码其实比你想象的还要繁琐。

更新

根据您更新的问题,最好的选择是将字符串变量放入名为的List中,例如ipaddr。然后要查看是否包含字符串 str,只需执行以下操作:

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

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. Then to see if the string str is included, simply do this:

if( ipaddr.Contains( str ) )
{
   //contained in the list
}
素食主义者 2024-10-28 06:55:28

我会做类似的事情:

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

编辑

更新后,我会做类似的事情:

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

为了获得更好的性能,请考虑使用 HashSet 而不是数组,特别是如果有效字符串列表不会改变。

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.

初与友歌 2024-10-28 06:55:28

更具可读性和更高的性能将是:(

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

尽管,不可否认的是非常冗长......)

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...)

温柔戏命师 2024-10-28 06:55:28

我会做一个

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

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

I would do a

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

if (variables.Contains(inputstring))
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文