如何从 C# 中的字符串中删除“\r\n”?我可以使用正则表达式吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1981947/
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
How can I remove "\r\n" from a string in C#? Can I use a regular expression?
提问by Hcabnettek
I am trying to persist string from an ASP.NET textarea. I need to strip out the carriage return line feeds and then break up whatever is left into a string array of 50 character pieces.
我正在尝试从 ASP.NET textarea 中保留字符串。我需要去掉回车换行符,然后将剩下的任何内容分解成一个由 50 个字符组成的字符串数组。
I have this so far
到目前为止我有这个
var commentTxt = new string[] { };
var cmtTb = GridView1.Rows[rowIndex].FindControl("txtComments") as TextBox;
if (cmtTb != null)
commentTxt = cmtTb.Text.Length > 50
? new[] {cmtTb.Text.Substring(0, 50), cmtTb.Text.Substring(51)}
: new[] {cmtTb.Text};
It works OK, but I am not stripping out the CrLf characters. How do I do this correctly?
它工作正常,但我没有去除 CrLf 字符。我该如何正确地做到这一点?
回答by Matt Greer
You could use a regex, yes, but a simple string.Replace() will probably suffice.
您可以使用正则表达式,是的,但是一个简单的 string.Replace() 可能就足够了。
myString = myString.Replace("\r\n", string.Empty);
回答by Matt McClellan
Assuming you want to replace the newlines with somethingso that something like this:
假设你想用一些东西替换换行符,这样的东西:
the quick brown fox\r\n
jumped over the lazy dog\r\n
doesn't end up like this:
不会像这样结束:
the quick brown foxjumped over the lazy dog
I'd do something like this:
我会做这样的事情:
string[] SplitIntoChunks(string text, int size)
{
string[] chunk = new string[(text.Length / size) + 1];
int chunkIdx = 0;
for (int offset = 0; offset < text.Length; offset += size)
{
chunk[chunkIdx++] = text.Substring(offset, size);
}
return chunk;
}
string[] GetComments()
{
var cmtTb = GridView1.Rows[rowIndex].FindControl("txtComments") as TextBox;
if (cmtTb == null)
{
return new string[] {};
}
// I assume you don't want to run the text of the two lines together?
var text = cmtTb.Text.Replace(Environment.Newline, " ");
return SplitIntoChunks(text, 50);
}
I apologize if the syntax isn't perfect; I'm not on a machine with C# available right now.
如果语法不完美,我深表歉意;我现在不在一台可以使用 C# 的机器上。
回答by Chris
This splits the string on any combo of new line characters and joins them with a space, assuming you actually do want the space where the new lines would have been.
这会在任何新行字符组合上拆分字符串,并用空格将它们连接起来,假设您确实想要新行所在的空间。
var oldString = "the quick brown\rfox jumped over\nthe box\r\nand landed on some rocks.";
var newString = string.Join(" ", Regex.Split(oldString, @"(?:\r\n|\n|\r)"));
Console.Write(newString);
// prints:
// the quick brown fox jumped over the box and landed on some rocks.
回答by dimazaid
The .Trim() function will do all the work for you!
.Trim() 函数将为您完成所有工作!
I was trying the code above, but after the "trim" function, and I noticed it's all "clean" even before it reaches the replace code!
我正在尝试上面的代码,但是在“修剪”功能之后,我注意到它甚至在到达替换代码之前都是“干净的”!
String input: "This is an example string.\r\n\r\n"
Trim method result: "This is an example string."
Source: http://www.dotnetperls.com/trim
回答by alonp
Nicer code for this:
更好的代码:
yourstring = yourstring.Replace(System.Environment.NewLine, string.Empty);
回答by Fred Conklin
Try this:
尝试这个:
private void txtEntry_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string trimText;
trimText = this.txtEntry.Text.Replace("\r\n", "").ToString();
this.txtEntry.Text = trimText;
btnEnter.PerformClick();
}
}
回答by vibs2006
Here is the perfect method:
这是一个完美的方法:
Please note that Environment.NewLineworks on on Microsoftplatforms.
请注意Environment.NewLine适用于Microsoft平台。
In addition to the above, you need to add \rand \nin a separatefunction!
除上述外,还需要在单独的函数中添加\r和\n!
Here is the code which will support whether you type on Linux, Windows, or Mac:
这是支持您在Linux、Windows 或 Mac上键入的代码:
var stringTest = "\r Test\nThe Quick\r\n brown fox";
Console.WriteLine("Original is:");
Console.WriteLine(stringTest);
Console.WriteLine("-------------");
stringTest = stringTest.Trim().Replace("\r", string.Empty);
stringTest = stringTest.Trim().Replace("\n", string.Empty);
stringTest = stringTest.Replace(Environment.NewLine, string.Empty);
Console.WriteLine("Output is : ");
Console.WriteLine(stringTest);
Console.ReadLine();
回答by Ramya Prakash Rout
Use:
用:
string json = "{\r\n \"LOINC_NUM\": \"10362-2\",\r\n}";
var result = JObject.Parse(json.Replace(System.Environment.NewLine, string.Empty));