C
C#2y ago
Stay

✅ Prevent carriage return from being added in the new multi-line string literal?

How to prevent \r from being added in a multi-line string literal?
"""
First Line
Second Line
"""
"""
First Line
Second Line
"""
C# is adding a \r after the \n only in Windows. I know that Windows uses \n\r to indicate new lines, but can i somehow disable that?
8 Replies
333fred
333fred2y ago
C# does no interpretation of the characters inside your string So, if the file literally has \r\n in the text there, that's what you get
Stay
StayOP2y ago
I'm not reading a file, i have a multiline string like this
var str = """
First Line
Second Line
"""
var str = """
First Line
Second Line
"""
And it is adding \n\r after First Line I can prevent this by doing
var str = """
First Line
Second Line
""".Replace("\r", "");
var str = """
First Line
Second Line
""".Replace("\r", "");
But maybe there is a cleaner solution
Aaron
Aaron2y ago
no like your source file has \r in it for the newlines in the string
Stay
StayOP2y ago
oh that... make sense
Aaron
Aaron2y ago
so C# preserves that \r
Stay
StayOP2y ago
My bad then, any idea if that can be changed in Rider?
Aaron
Aaron2y ago
yeah, should be a button for line endings on the bottom right I think
Stay
StayOP2y ago
Yep just found it, changed it to LF instead lf CRLF, thank your very much 😅
Want results from more Discord servers?
Add your server