0

again a pretty simple question(i think so) but i can't find something via google. I think i'm using the wrong keywords but i don't know how to google it right.

So what i want to do is: I have a text box (just a normal textbox not a richtextbox), the input text is coming from a database. Example String: "TextA \nTextB"

If i read out the string from the textbox the output is: "TextA \\nTextB". I'm creating a pdf with the output from the textbox, so actual in the pdf you can read "TextA \nTextB", but there is no new line. If i create a normal string with "TextA \nTextB". I can see the new line in the pdf.

So basicly the question is: What i have to change, that mit output string is the same as the input string?

What i already tried: TextA \r\nTextB TextA'\n'TextB TextA"\n"TextB

Thank you

TheRealLife
  • 375
  • 4
  • 20
  • Environment.NEwLine..try that – Pranay Rana May 24 '18 at 07:43
  • 1
    I need to be very clear; does the string from the database contain a newline, or does it contain a slash and an n? Does `int i = theString[6]` come out as 10? or 92? – Marc Gravell May 24 '18 at 07:43
  • 1
    Environment.NewLine – Eldho May 24 '18 at 07:43
  • Issues with newlines are notoriously difficult to diagnose just from *descriptions*, especially since e.g. what the *debugger* shows you is "what you would have to write in source code", not what the string actually "is". We could really do with some *code* to repro the issue, which hopefully can be done in a small sample without having to get the PDF side actually involved. – Damien_The_Unbeliever May 24 '18 at 07:44
  • @MarcGravell actual it contains a slash n. but i can't change it to what ever i want – TheRealLife May 24 '18 at 07:44
  • 2
    If it's winforms, the TextBox have a bool property called `Multiline`.if it's asp.net, the TextBox have a property called `TextMode`. If it's wpf, the TextBox have `AcceptsReturn` and `TextWrapping` properties... – Zohar Peled May 24 '18 at 07:46
  • Marc is saying that `\n` can be either a NewLine char (in SQL it's char(13)), or simplly a slash `\` followed by `n` (as in, two different chars). The first will cause a new line, the second will not. So which one is it in your case? – Zohar Peled May 24 '18 at 07:49
  • 1
    @ZoharPeled minor point: newline is 10; carriage return is 13 – Marc Gravell May 24 '18 at 07:52
  • @MarcGravell it returns a 92 – TheRealLife May 24 '18 at 07:53
  • @ZoharPeled thanks for explaining this. I already tried the multiline flag, nothing changed – TheRealLife May 24 '18 at 07:54
  • 1
    @TheRealLife a slash, then; yeah, that's simply not a newline in any way - it would be *incorrect* to represent it as a newline – Marc Gravell May 24 '18 at 07:55
  • @MarcGravell Thanks! I always get them backwords. In fact, I'm so used to get them backwords that my first though was that it's 10 so I wrote 13 :-) – Zohar Peled May 24 '18 at 08:00

1 Answers1

4

(from comments)

actual it contains a slash n

and

it returns a 92

(where "it" is the character code at the index of the ...\n..., so: a slash)

If the contents are a slash and an n, then: that isn't a newline, and the tools are doing the correct thing by representing it as a slash and an n. The \n syntax only applies to C# literals, as a way of telling the compiler to include something that isn't actually a slash and an n.

A C# literal expressed as "foo\nbar" is the string with contents:

foo
bar

(a newline)

However, a string with contents foo\nbar is the string with contents:

foo\nbar

(a slash and an n, no newlines)

For completeness, there's also C# verbatim string literals, expressed as @"foo\nbar" which is also the string with contents

foo\nbar

(a slash and an n, no newlines)

When you load a string value from the database, you aren't using C# literals - you are just dealing with strings. So a slash and an n means: a slash and an n. Not a newline.

So: if possible, store what you actually mean. The text in the database should contain a newline, not a slash and an n. If the line-breaks don't show as line breaks when you do select TheValue from TheTable, it isn't stored correctly.

If that isn't an option, you'll have to use some kind of text replace (maybe .Replace(@"\r\n","\r\n").Replace(@"\n", "\n").Replace(@"\r", "\r")), but you'll need to think about false positives, especially if your context can include things like code examples, i.e. slash followed by n is likely to appear to mean a slash followed by n.


In terms of SQL literals; if you wanted to do this in SQL, SQL literals allow newlines directly, so:

declare @someString nvarchar(200) = N'foo
bar'; -- this contains a newline

(although whether it contains CR, LF, CRLF or LFCR may depend on your editor's line-endings)

However, usually you would be populating values by SQL parameters, i.e.

cmd.CommandText = "... @val ..."; // some SQL
cmd.Parameters.AddWithValue("@val", someStringThatHasNewlines);
cmd.ExecuteNonQuery();

where someStringThatHasNewlines contains newline characters (ASCII 10/13), not slash+n.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    And, of course, all of the problems that arise if you want to support double backslash meaning a literal backslash. (double backslash followed by n shouldn't produce a newline that naive replacement could cause) – Damien_The_Unbeliever May 24 '18 at 07:57
  • 2
    @Damien_The_Unbeliever I assume there you're talking about the last paragraph - the pain of trying to do a replace? If so, yeah, that's an important additional pain point. – Marc Gravell May 24 '18 at 08:01
  • Thanks a lot for you example and your time. I tried to add a new line in the sql string. In this thread https://stackoverflow.com/questions/31057/how-to-insert-a-line-break-in-a-sql-server-varchar-nvarchar-string they are writing that i have to change my text to 'TextA' + CHAR(13) + 'TextB'. if I'm doing this. The text is exactly the same... So in my database give a this string back: "'TextA '+ CHAR(13)+ 'TextB'". I edited the database text directly via HeidiSQL. Was that wrong? – TheRealLife May 24 '18 at 08:09
  • @TheRealLife I've added a note on SQL literals at the bottom of the post - impossible to format correctly in comments – Marc Gravell May 24 '18 at 08:19
  • @MarcGravell Thanks a lot – TheRealLife May 24 '18 at 08:21