C# 中的 String.Replace(char, char) 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/651702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String.Replace(char, char) method in C#
提问by sarsnake
How do I replace \n
with empty space?
如何\n
用空白替换?
I get an empty literal error if I do this:
如果我这样做,我会得到一个空的文字错误:
string temp = mystring.Replace('\n', '');
采纳答案by Samuel
String.Replace('\n', '')
doesn't work because ''
is not a valid character literal.
String.Replace('\n', '')
不起作用,因为''
不是有效的字符文字。
If you use the String.Replace(string, string) override, it should work.
如果您使用 String.Replace(string, string) 覆盖,它应该可以工作。
string temp = mystring.Replace("\n", "");
回答by Robin Day
This should work.
这应该有效。
string temp = mystring.Replace("\n", "");
Are you sure there are actual \n new lines in your original string?
您确定原始字符串中有实际的 \n 新行吗?
回答by BFree
string temp = mystring.Replace("\n", " ");
回答by Steven Evers
One caveat: in .NET the linefeed is "\r\n". So if you're loading your text from a file, you might have to use that instead of just "\n"
一个警告:在 .NET 中,换行符是“\r\n”。因此,如果您要从文件加载文本,则可能必须使用它,而不仅仅是 "\n"
edit> as samuel pointed out in the comments, "\r\n" is not .NET specific, but is windows specific.
edit> 正如塞缪尔在评论中指出的那样,“\r\n”不是特定于 .NET 的,而是特定于 Windows 的。
回答by Patrick McDonald
If you use
如果你使用
string temp = mystring.Replace("\r\n", "").Replace("\n", "");
then you won't have to worry about where your string is coming from.
那么你就不必担心你的字符串来自哪里。
回答by MRFerocius
What about creating an Extension Method like this....
创建这样的扩展方法怎么样....
public static string ReplaceTHAT(this string s)
{
return s.Replace("\n\r", "");
}
And then when you want to replace that wherever you want you can do this.
然后当你想在任何你想要的地方替换它时,你可以这样做。
s.ReplaceTHAT();
Best Regards!
此致!
回答by JeffH
@gnomixa - What do you mean in your comment about not achieving anything? The following works for me in VS2005.
@gnomixa - 你在评论中关于没有取得任何成就是什么意思?以下在 VS2005 中对我有用。
If your goal is to remove the newline characters, thereby shortening the string, look at this:
如果您的目标是删除换行符,从而缩短字符串,请查看以下内容:
string originalStringWithNewline = "12\n345"; // length is 6
System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
string newStringWithoutNewline = originalStringWithNewline.Replace("\n", ""); // new length is 5
System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 5);
If your goal is to replace the newline characters with a space character, leaving the string length the same, look at this example:
如果您的目标是用空格字符替换换行符,使字符串长度保持不变,请查看以下示例:
string originalStringWithNewline = "12\n345"; // length is 6
System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
string newStringWithoutNewline = originalStringWithNewline.Replace("\n", " "); // new length is still 6
System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 6);
And you have to replace single-character strings instead of characters because '' is not a valid character to be passed to Replace(string,char)
并且您必须替换单字符串而不是字符,因为 '' 不是要传递给 Replace(string,char) 的有效字符
回答by Guffa
As replacing "\n" with "" doesn't give you the result that you want, that means that what you should replace is actually not "\n", but some other character combination.
由于用 "" 替换 "\n" 不会给你想要的结果,这意味着你应该替换的实际上不是 "\n",而是一些其他的字符组合。
One possibility is that what you should replace is the "\r\n" character combination, which is the newline code in a Windows system. If you replace only the "\n" (line feed) character it will leave the "\r" (carriage return) character, which still may be interpreted as a line break, depending on how you display the string.
一种可能是你应该替换的是“\r\n”字符组合,它是Windows系统中的换行符。如果仅替换“\n”(换行)字符,它将留下“\r”(回车)字符,根据您显示字符串的方式,该字符仍可能被解释为换行符。
If the source of the string is system specific you should use that specific string, otherwise you should use Environment.NewLine to get the newline character combination for the current system.
如果字符串的来源是特定于系统的,则应使用该特定字符串,否则应使用 Environment.NewLine 获取当前系统的换行符组合。
string temp = mystring.Replace("\r\n", string.Empty);
or:
或者:
string temp = mystring.Replace(Environment.NewLine, string.Empty);
回答by cilerler
Here is your exact answer...
这是你的确切答案...
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
LineFeed
).Replace(mystring, string.Empty);
But this one is much better... Specially if you are trying to split the lines (you may also use it with Split)
但是这个要好得多......特别是如果你试图分割线(你也可以将它与Split一起使用)
const char CarriageReturn = '\r'; // #13
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
string.Format("{0}?{1}", CarriageReturn, LineFeed)
).Replace(mystring, string.Empty);