69

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.

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190

9 Answers9

128

You could use a regex, yes, but a simple string.Replace() will probably suffice.

 myString = myString.Replace("\r\n", string.Empty);
Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • 6
    Environment.NewLine would be the server's idea of what a new line is. This text is coming from the client, which may use different characters for newlines. Here is an answer that shows what different browsers use for new lines: http://stackoverflow.com/questions/1155678/javascript-string-newline-character/1156388#1156388 – Matt Greer Dec 30 '09 at 19:59
  • 4
    Put a pipe between them and you can do it in one line `myString.Replace("\r|\n", string.Empty);` – David Lilljegren Dec 07 '17 at 18:03
  • 2
    Sorry that should have been `Regex.Replace(myString, "\n|\r", String.Empty);` then it will replace both in one go – David Lilljegren Dec 07 '17 at 18:27
62

The .Trim() function will do all the work for you!

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dimazaid
  • 1,673
  • 3
  • 22
  • 24
30

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.
Chris
  • 27,596
  • 25
  • 124
  • 225
27

Nicer code for this:

yourstring = yourstring.Replace(System.Environment.NewLine, string.Empty);
alonp
  • 1,307
  • 2
  • 10
  • 22
5

Here is the perfect method:

Please note that Environment.NewLine works on on Microsoft platforms.

In addition to the above, you need to add \r and \n in a separate function!

Here is the code which will support whether you type on Linux, Windows, or 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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vibs2006
  • 6,028
  • 3
  • 40
  • 40
3

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();
        }
    }
Will Sewell
  • 2,593
  • 2
  • 20
  • 39
0

Assuming you want to replace the newlines with something so 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.

Matt McClellan
  • 1,639
  • 1
  • 10
  • 12
0

Use:

string json = "{\r\n \"LOINC_NUM\": \"10362-2\",\r\n}";
var result = JObject.Parse(json.Replace(System.Environment.NewLine, string.Empty));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Use double \ or use @ before " for find \r or \n

htmlStr = htmlStr.Replace("\\r", string.Empty).Replace("\\n", string.Empty);
htmlStr = htmlStr.Replace(@"\r", string.Empty).Replace(@"\n", string.Empty);
M Komaei
  • 7,006
  • 2
  • 28
  • 34