如何去除" "“和“;;从一个字符串

本文关键字:quot 字符串 一个 何去 | 更新日期: 2023-09-27 18:11:37

我有一个字符串<hello>。我想去掉< and >。我尝试了remove(),但它不工作。

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

但是,它不工作。如何从字符串中移除< and > ?

如何去除" "“和“;;从一个字符串

听起来你想要Trim方法:

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

你可以这样做:

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

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

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

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

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

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