简体   繁体   中英

C# string.ALL<> using contain to search a string - htmlagility

I'm trying to scrape HTML file using htmlagility pack. I realize this is not issue with agility pack but related to understanding string manipulation, as per example code below, i'm expecting "not found" but it is returning as Found. It appears like searching as char instead of string. If I put XYZ in str1, result is not found. Can someone help me to how to get the desire result.

private void button2_Click(object sender, EventArgs e)
    {
        string str1 = "PBS" ;
        string str2 = "JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ";

        if ( str1.All(x=> str2.Contains(x))  )
        {
            MessageBox.Show(str2);
        }
        else
        {
            MessageBox.Show("Not Found");
        }

    }

My actual coding as below and i'm also getting html node that I don't want.


string monthValues = " JAN FEB MAR APR MAY JUN JUL AUG OCT NOV DEC ";
var header = ROWS[rowCount + 1].Descendants("TH").Where(node => node.InnerHtml.ToString().Substring(0,2).All(x => monthValues.Contains(x))).ToList();

and html as follow. I'm also getting "TH" node which has "PBS"

<tr>
<th class="l t colhd" rowspan="11"> 9494Q     </th>
<th class="l t colhd" rowspan="3">PBS </th>
<th class="l t colhd">FEB2021</th>
</tr>

Appreciate your help.

Regards

The code is looking for each individual character from str1 within str2. Hence the letters PB and S all appear in str2. XY and Z do not.

I think you simply want

if (str2.Contains(str1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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