简体   繁体   中英

C# Split and get value

I have a little problem with getting items from webbrowser.document.

the part of code in document tha i need is this>

primary-text,7.gm2-body-2">**ineedthis.se**</div> <div jstcache="194" 

I need to parse the "ineedthis.se" that will be different every time.

my code is this

if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
                {
                   System.IO.StreamReader sr = new System.IO.StreamReader(webBrowser1.DocumentStream.ToString());
                    string rssourcecode = sr.ReadToEnd();
                     Regex r = new Regex("7.gm2-body-2 > </ div > < div ", RegexOptions.Multiline);
                    MatchCollection matches = r.Matches(rssourcecode);
                    // Dim r As New System.Text.RegularExpressions.Regex("here need splitersorsomething", RegexOptions.Multiline)
                    foreach (Match itemcode in matches)
                    {
                        listBox1.Items.Add(itemcode.Value.Split(here need splites).GetValue(2));
                    }
                   
                }

so. can you please help me with right splitters ? thanks a lot

Your question was not very clear,still what I could understand is you want to get a part of a string(Substring) from a string.

Assuming you have a string value saved in variable:

string stringValue = primary-text,7.gm2-body-2">**ineedthis.se**</div> <div jstcache="194";

And you want to extract is: ineedthis.se

So you can try out stringValue.Substring(31,44); .

You can take reference from : https://www.c-sharpcorner.com/UploadFile/mahesh/substring-in-C-Sharp/

Let's say your string is following

var str = "<div primary-text,7.gm2-body-2\">**some random text**</div> <div jstcache=\"194\""; 

The below is a very rudimentary solution but will help you in your answer

var foundStart = false;
var foundEnd = false;
var startIndex = -1;
var length = 0;
for(var index = 0; index < str.Length; index++)
{
    if (!foundStart)
    {
        if (str[index].Equals('<') && str.Substring(index, 4).Equals("<div"))
        {
            foundStart = true;                  
            continue;
        }
    }

    if (foundStart && !foundEnd)
    {
        if (str[index].Equals('>'))
        {
            foundEnd = true;
            continue;
        }
    }

    if (foundStart && foundEnd)
    {   
        if (startIndex == -1)
            startIndex = index;
        else
            length++;
            
        if (str[index + 1].Equals('<'))
        {
            foundStart = false;
            foundEnd = false;
            break;
        }
    }
}

//// This is your answer
Console.WriteLine(str.Substring(startIndex, length));

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