按字符串修整字符串
你可以使用HttpUtility.HtmlDecode(String)
和使用结果作为一个输入String.Trim()
HttpUtility.HtmlDecode on MSDN
HttpServerUtility.HtmlDecode on MSDN(一个包装就可以通过访问Page.Server属性)
string stringWithNonBreakingSpaces;
string trimmedString = String.Trim(HttpUtility.HtmlDecode(stringWithNonBreakingSpaces));
注:这soluti on会解码输入中的所有HTML字符串。
修剪方法默认从当前字符串中删除所有前导和尾随空白字符。
编辑:解决你的问题在您编辑后:
string input = @" <a href='#'>link</a> ";
Regex regex = new Regex(@"^( |\s)*|( |\s)*$");
string result = regex.Replace(input, String.Empty);
这将删除所有尾随和前导空格和
。您可以将任何字符串或字符组添加到表达式。如果你要修剪所有选项卡太正则表达式会简直成了:
Regex regex = new Regex(@"^( |\s|\t)*|( |\s|\t)*$");
不知道这是你在找什么?
string str = "hello ";
str.Replace(" ", "");
str.Trim();
这将替换到处,而不是在开始 – 2012-04-17 08:53:42
使用正则表达式,为大卫·赫弗南说。这是比较容易选择的字符串开头的所有空格:^(\ | )*
他想在开始和结束时修剪字符串 – 2012-04-17 09:10:38
这只是一个提示,而不是完整的解决方案 – 2012-04-17 09:13:02
将是一个简单的正则表达式 – 2012-04-17 08:46:40