我有一个使用 linq-to-XML 读取的 xml 文件。 Linq-to-XML 保留换行符和缩进空格。
所以不要使用这样的 guid:
"FB5417E2-DB15-481d-80D6-D0C4CB26EB1F"
I am getting stuff like this:
"\n FB5417E2-DB15-481d-80D6-D0C4CB26EB1F"
I have made this method to try and help compensate for this:
public static string ValueTrimmed(this XElement element)
{
if (element != null)
// Remove the newlines and spaces
return element.Value.Replace("\n ", "");
return "";
}
问题是这只适用于 "\n"+ 6 个空格。
有没有办法删除“\n”+任意数量的空格?
注意:在某些情况下,“\n”+ x 空格位于值的内部。
例如:
TextTextTextTextTextTextText\n TextTextTextTextTextTextText
最佳答案
删除所有后跟空格的换行符:
return Regex.Replace(element.Value, @"\n\s*", String.Empty);
如果你想在行与行之间保留一个空格:
return Regex.Replace(element.Value, @"\n\s*", " ").Trim();
关于c# - 用空字符串替换可变字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3990917/