gpt4 book ai didi

c# - 可以在开关 {case} 中使用通配符或字符串 "contains"吗?注意 : wordy

转载 作者:行者123 更新时间:2023-11-30 19:32:18 25 4
gpt4 key购买 nike

作为编程新手,我阅读了很多示例代码,并尝试将所有东西组合在一起,以了解哪些是可行的。我正在使用 html agility pack 尝试删除新闻网页。

问题:我正在测试的节点之一不使用静态值,它使用查看时间。我如何将其应用于 switch {case} 方法。如果我在整个方法中偏离基础,我也愿意接受任何建议。

另请注意:我不需要捕获此节点,如果有适合我的跳过它的方法。

我决定使用一个使用开关的例子,

var rows = doc.DocumentNode.SelectNodes(".//*[@id='weekdays']/tr");
foreach (var row in rows)
{
var cells = row.SelectNodes("./td");
string title = cells[0].InnerText;
var valueRow = cells[2];
switch (title)
{
case "Date":
HtmlNode date = valueRow.SelectSingleNode("//*[starts-with(@id, 'detail_row_seek')]/td");
Console.WriteLine("UPC=A:\t" + date.InnerText);
break;

case "":
string Time = valueRow.InnerText;
Console.WriteLine("Time:\t" + Time);
break;


case "News":
string Time = valueRow.InnerText;
Console.WriteLine("News:\t" + News);
break;
}

html 摘录

<table id="weekdays" cellpadding="6" cellspacing="0" border="0" width="100%">
<tr>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; text-align: center; width:8%; padding-left: 6px;">Date</td>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; width:8%; text-align: center; white-space:nowrap"><a href="guestcp.php?do=customoptions" title="Time &amp; Date Options"><img style="position:relative; vertical-align: bottom;" src="images/misc/clock_small.gif" title="Time &amp; Date Options" alt="Time &amp; Date Options" border="0" /></a><a href="guestcp.php?do=customoptions" title="Time &amp; Date Options"><span id="ff_nowtime_clock">3:20pm</span></a></td>
<td class="thead" style="border-bottom: 1px solid #d1d1e1;font-weight:normal; text-align: center; width:8%;">News</td>

…………

                    <tr id="detail_row_seek_37876">

<td id="toprow_9" class="alt1 espace" rowspan="3" style="vertical-align: top; text-align: center;" nowrap="nowrap">
<span class="smallfont">
<div>Sat</div>
Apr 9
</span>
</td>

<td class="alt1 espace" style="text-align: center;" nowrap="nowrap">

<span class="smallfont">Day 3</span>


</td>
<td class="alt1 espace" style="text-align: center;"><span class="smallfont">EUR</span></td>
<td class="alt1 espace" style="padding-top: 2px" align="center">


<a name="chart=37876" style="position:absolute; margin-top: -10px;"></a><a name="details=37876" style="position:absolute; margin-top: -10px;"></a>


<div class="cal_imp_medium" title="Medium Impact Expected"></div></td>
<td class="alt1 espace">

<div class="smallfont" id="title_37876" style="padding-left: 11px;">ECOFIN Meetings</div>

</td>

问题是:所谓的时间列不是静态的,它实际上使用时间值。有没有办法在案例中使用通配符或使用“包含”来解决这个非常冗长的问题?

最佳答案

在 switch 语句的每种情况下都必须使用常量值。

我能想到的让你做你想做的事情的唯一方法是使用 default: 案例 - 在这个默认案例中你可以测试你正在寻找的值containsParseRegex 使用 if 进行测试。

我不能完全理解您的 HTML 示例代码(抱歉!)- 但修改后的 C# 可能类似于:

            switch (title)
{
case "Date":
HtmlNode date = valueRow.SelectSingleNode("//*[starts-with(@id, 'detail_row_seek')]/td");
Console.WriteLine("UPC=A:\t" + date.InnerText);
break;


case "News":
string News = valueRow.InnerText;
Console.WriteLine("News:\t" + News);
break;

default:
if (regexTime.Match(title))
{
string Time = valueRow.InnerText;
Console.WriteLine("Time:\t" + Time);
}
break;
}

关于c# - 可以在开关 {case} 中使用通配符或字符串 "contains"吗?注意 : wordy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5608541/

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