C#拆分并获取值

C#拆分并获取值,c#,C#,我在从webbrowser.document获取项目时遇到一点问题 文档中我需要的代码部分是this> primary-text,7.gm2-body-2">**ineedthis.se**</div> <div jstcache="194" primary text,7.gm2-body-2”>**ineed这个.se**你的问题不是很清楚,但我能理解的是你想从字符串中得到字符串的一部分(子字符串) 假设变量中保存了字符串值: stri

我在从webbrowser.document获取项目时遇到一点问题

文档中我需要的代码部分是this>

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

primary text,7.gm2-body-2”>**ineed这个.se**你的问题不是很清楚,但我能理解的是你想从字符串中得到字符串的一部分(子字符串)

假设变量中保存了字符串值:

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

string stringValue=primary text,7.gm2-body-2“>**inedthis.se**假设您的字符串如下

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

var str=“欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后,你的问题包括你的源代码作为一个,它可以被其他人编译和测试。另外,请检查以了解您可以提出哪些问题。请参阅:。也请阅读答案。您是否阅读了Luuk评论中的页面?最好获取ineedthis的索引。请设置索引,然后在其上添加子字符串。感谢您的回复。但这不是一个解决方案。也许我没有解决我的问题。在webbrowser.document(HMTL)中,ia有很多代码,在本例中,我们不知道字符串“primary text,7.gm2-body-2”>**在这个.se**OP中有一些大量HTML,可能有许多
div
元素。他特别在找这个
div
。您将获得任何
div
.Enigmativity。对没错。非常感谢。我在html正文中有很多div,但只需要去掉它。
var str = "<div primary-text,7.gm2-body-2\">**some random text**</div> <div jstcache=\"194\""; 
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));