如何从字符串中获取“标签”?

时间:2019-09-29 22:52:48

标签: c# tags

请注意,我不是英语,所以很难解释。

我需要从字符串中获取HTML-Like标签。有官方的方法吗?

我已经成功获得了字符串的前3个字母,但是我只能在标记内使用一个字母。

string tag = command.Substring(0, 3); // Gets the first three letters (The tag)
command = command.Substring(3); // Removes the tag from the string.

if(tag == "<x>")
{
    // Do stuff.
}

此代码可以正常工作,但将我限制为只能使用一个字母。 我是否仍然可以使用<hello>这样的标签?

非常感谢您。

编辑:

对不起,有混乱。我的要求是我可以输入一个字符串,程序将从字符串的开头获取标签。例如: >> <abc>This is a string 程序将找到标签(<abc>)。 这本来应该是 HTML,但实际上并不相关。

再次,抱歉我的语法和解释不正确。

1 个答案:

答案 0 :(得分:0)

那又怎么样:

//Suppose that the source string is:

string src = "<Hello> The rest of the string....";

//To extract the tag <Hello> from the source string:

string tag = src.Substring(0, src.IndexOf(">") + 1);

if(tag == "<Hello>")
{
 //do some..
}

希望有帮助。