gpt4 book ai didi

c# - 写长的if语句更简洁

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:02 24 4
gpt4 key购买 nike

这么长的if语句有没有更好的写法?唯一改变的是条件中的 d=...

<%
if (
Request.Url.ToString().Contains("d=36423")
|| Request.Url.ToString().Contains("d=39252")
|| Request.Url.ToString().Contains("d=39280")
|| Request.Url.ToString().Contains("d=39251")
|| Request.Url.ToString().Contains("d=39246")
|| Request.Url.ToString().Contains("d=39245")
|| Request.Url.ToString().Contains("d=39244")
|| Request.Url.ToString().Contains("d=39243")
|| Request.Url.ToString().Contains("d=39242")
|| Request.Url.ToString().Contains("d=39241")
|| Request.Url.ToString().Contains("d=39240")
|| Request.Url.ToString().Contains("d=26009")
|| Request.Url.ToString().Contains("d=39239")
|| Request.Url.ToString().Contains("d=39238")
|| Request.Url.ToString().Contains("d=39237")
|| Request.Url.ToString().Contains("d=38816")
)
{%>

<div class="page-desktop-nav">

<%} else {%>
<div class="mobile-nav">
<%}%>

最佳答案

您可以将值存储在数组中并使用类似以下内容:

<%  
var arr = new string[]
{
"36423",
"39252",
"39280",
"39251",
"39246",
"39245",
"39244",
"39243",
"39242",
"39241",
"39240",
"26009",
"39239",
"39238",
"39237",
"38816"
};

string url = Request.Url.ToString();
if (arr.Any(s => url.Contains(string.Format("d={0}", s)))) {
%>

关于c# - 写长的if语句更简洁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com