使用多个搜索项和一次迭代来查找String中的索引

时间:2018-12-16 19:10:48

标签: c# string linq

我有以下HTML示例文档:

.....
<div class="TableElement">
    <table>
    <tr>
        <th class="boxToolTip" title="La quotazione di A2A è in rialzo o in ribasso?">&nbsp;</th>
        ..
        <th class="boxToolTip" class="ColumnLast" title="Trades più recenti su A2A">Ora <img title='' alt='' class='quotePageRTupgradeLink' href='#quotePageRTupgradeContainer' id='cautionImageEnt' src='/common/images/icons/caution_sign.gif'/></th>
    </tr>
    <tr class="odd">
        ..
        <td align="center"><span id="quoteElementPiece6" class="PriceTextUp">1,619</span></td>
        <td align="center"><span id="quoteElementPiece7" class="">1,6235</span></td>
        <td align="center"><span id="quoteElementPiece8" class="">1,591</span></td>
        <td align="center"><span id="quoteElementPiece9" class="">1,5995</span></td>
        ..
    </tr>
    </table>
</div>
......

我需要获取quoteElementPiece 6,7,8,9和17(当前在文档中进一步介绍)部分对应的值。

我现在只是在代码中一个接一个地搜索:

int index6 = doc.IndexOf("quoteElementPiece6");
..
int index17 = doc.IndexOf("quoteElementPiece17");

我想通过一次扫描并使我需要的所有子字符串的所有索引得到改善。示例:

var searchstrings = new string[]
{
    "quoteElementPiece6",
    "quoteElementPiece7",
    "quoteElementPiece8",
    "quoteElementPiece9",
    "quoteElementPiece17"
};

int[] indexes = getIndexes(document,searchstrings); //indexes should be sorted accordingly to the order in searchstrings

.NET中是否有任何本地工具(LinQ表示这样)?

我知道有HTML解析器库,但是我更喜欢避免使用它们,我想学习如何对每种文档进行此操作。

3 个答案:

答案 0 :(得分:2)

var words = new []{
    "quoteElementPiece6",
    "quoteElementPiece7"};      
// I take for granted your `document` is a string and not an `HtmlDocument` or whatnot.
var result = words.Select(word=>document.IndexOf(word));
Console.WriteLine(string.Join(",", result));

答案 1 :(得分:0)

您可以使用LINQ做到这一点。检查我的解决方案

var doc = "this is my document";
List<string> searchstrings = new List<string>
{
    "quoteElementPiece6",
    "quoteElementPiece7",
    "quoteElementPiece8",
    "quoteElementPiece9",
    "quoteElementPiece17"
};
var lastIndexOfList = new List<int>(searchstrings.Count);

searchstrings.ForEach(x => lastIndexOfList.Add(doc.LastIndexOf(x)));

答案 2 :(得分:0)

var pattern = @"(?s)<tr class=""odd"">.+?</tr>";
var tr = Regex.Match(html, pattern).Value.Replace("&nbsp;", "");
var xml = XElement.Parse(tr);
var nums = xml
            .Descendants()
            .Where(n => (string)n.Attribute("id") != null)
            .Where(n => n.Attribute("id").Value.StartsWith("quoteElementPiece"))
            .Select(n => Regex.Match(n.Attribute("id").Value, "[0-9]+").Value);