0

I am having a heck of a time replacing a newline character in a string inside on Azure Function. This code runs fine as console app

string str = "24\n";
str = str.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace(System.Environment.NewLine, string.Empty)

But inside of Azure Function the \n does not go away. Are there alternative methods to replacing a newline other than System.Environment.NewLine?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Isaac Levin
  • 2,809
  • 9
  • 49
  • 88
  • check here: https://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c – Aaron Nov 29 '17 at 15:08
  • 1
    You need to assign it to `str`... `str = str.Replace....` – Gilad Green Nov 29 '17 at 15:11
  • How do you see it doesn't work in Azure Function? – Mikhail Shilkov Nov 29 '17 at 15:11
  • 2
    When you have replaced \r and \n, you don't need to replace the combination (Environment.Newline) also – Hans Kesting Nov 29 '17 at 15:25
  • @GiladGreen the reassignment is there in code, just forgot to put in sample. – Isaac Levin Nov 29 '17 at 15:29
  • @Mikhail not working means if I run the function locally the string is unchanged after above code sample – Isaac Levin Nov 29 '17 at 15:29
  • @HansKesting I now that, but I was hoping that would handle all situations – Isaac Levin Nov 29 '17 at 15:29
  • 2
    Is the current edit closer to the real code? You do *not* want that `@` in front of the "\n". You *do* want to interpret that as newline, instead of backslash+n – Hans Kesting Nov 29 '17 at 15:32
  • @HansKesting it is exact now – Isaac Levin Nov 29 '17 at 15:41
  • 2
    No repro. The code removes the newline. There's nothing wrong with replace. .NET does recognize `\r` and `\n`. Throwing replacements "hoping it would handle all situations" when there's no problem to begin will only lead to trouble. What are you trying to do? What is the actual string that you want to replace and how do you know that the replacement failed? Are you sure there *is* a newline in there? – Panagiotis Kanavos Nov 29 '17 at 15:48
  • 4
    A lot of times people confuse the debuger's display for the actual string and think their string contains a backslash when it's only an escaped character like .. a newline. Perhaps the *opposite* happened here? The string *does* contain a backslash and n when it should contain a newline? – Panagiotis Kanavos Nov 29 '17 at 15:50
  • When running on Azure Functions, is this the EXACT code you have? Or are you taking the string input from a binding or some other input? How are you debugging this and observing the behavior? – Fabio Cavalcante Nov 29 '17 at 17:51
  • The last replace feels kind of redundant. What other common newline characters are there? – Manfred Radlwimmer Nov 30 '17 at 14:22

0 Answers0