如何删除“<”和“>”从一个字符串?

时间:2013-05-01 17:33:33

标签: c# asp.net

我有一个字符串<hello>。我想删除< and >。我尝试使用remove(),但它无效。

string str = "<hello>";
string new_str = str.Remove(str.Length-1);

但是,它没有用。如何从字符串中删除< and >

4 个答案:

答案 0 :(得分:10)

听起来像你想要的Trim方法:

new_str = str.Trim('<', '>');

答案 1 :(得分:6)

你可以这样做:

str = str.Replace("<", "").Replace(">", "");

答案 2 :(得分:1)

str = str.Replace("<", string.Empty).Replace(">", string.Empty);

答案 3 :(得分:0)

如果您只想删除第一个和最后一个字符,请尝试:

string new_str = (str.StartsWith("<") && str.EndsWith(">")) ? str.SubString(1, str.Length - 2) : str;

如果必须删除所有开始和结束字符:

string new_str = strTrim('<', '>');

否则

string new_str = str.Replace("<", "").Replace(">", "");
相关问题