0

I am trying to create multi-line string from string array like

@"A minErr message has two parts: 
  the message itself and the url that contains the encoded message.
  The message's parameters can contain other error messages which also include error urls.";

and I have string array of these line

string [] miltilines={"A minErr message has two parts:","the message itself and the url that contains the encoded message.","The message's parameters can contain other error messages which also include error urls."}

I have tried multiple approaches to get multiline string object but ended with \r\n in string object

1: String.Join(Environment.NewLine, miltilines)

2:

string multiline = @" ";
foreach (var item in miltilines)
            {
                multiline += @"" + item.Trim() + " ";
            }

3:

StringBuilder stringBuilder = new StringBuilder();
 foreach (var item in miltilines)
            {
 stringBuilder.AppendLine(item );
}

Is there any way to get multi line string object from string array

Muhammad Mansoor
  • 103
  • 1
  • 1
  • 8
  • #1 should have worked fine. What makes you think it didnt – pm100 Oct 15 '18 at 18:51
  • I have tried that and in object I am getting "A minErr message has two parts:/r/n the message itself and the url that contains the encoded message./r/n The message's parameters can contain other error messages which also include error urls." – Muhammad Mansoor Oct 15 '18 at 18:52
  • 9
    `but ended with \r\n in string object` that's how a line break is represented in visual studio debug windows. If you write that string to a file and open in a text editor, you will see the line break. – Magnetron Oct 15 '18 at 18:52
  • That said, either methods 1 or 3 should work. – Magnetron Oct 15 '18 at 18:55
  • Yes. A three line string object looks like "Line 1\r\nLine 2\r\nLine 3". The `\r\n` characters represent the line end characters in Windows. If the array has more than just a few lines, you should really be using the `StringBuilder` solution – Flydog57 Oct 15 '18 at 18:58
  • "\" starts a escape sequence, [here is a list](https://msdn.microsoft.com/en-us/library/h21280bw.aspx). You can use those in you string if you don't use `@` before the string. – Magnetron Oct 15 '18 at 18:58
  • I know i'll be getting that \r\n.. but what if I want that string object to be formatted as we do @" " .. I am using 3rd party tool that handle string in that format it is giving me exception when I am passing \r or \n – Muhammad Mansoor Oct 15 '18 at 19:15
  • 1
    If the third party tool gives you an exception when a string contains the newline character, then you can't pass in a multi-line string. A multi-line string *by definition* contains `\r\n`. – Rufus L Oct 15 '18 at 19:15
  • I need to pass as string object I am not writing these line any where and I am not sure about how many line I can have in that array – Muhammad Mansoor Oct 15 '18 at 19:16
  • It is working when I am passing hard coded string object with multi lines as I have in example – Muhammad Mansoor Oct 15 '18 at 19:17
  • 1
    The string formed from your sample also has `\r\n` in it. You've just used the `@` to hide the characters and show the formatted string in the editor. Set a breakpoint after that line and look at the string in the debugger. – Rufus L Oct 15 '18 at 19:22
  • Perhaps you should share the error message or stack trace that you're getting from the client when you pass in a string from #1 or #3, or just compare the two strings in the debugger to see if there are other differences. – Rufus L Oct 15 '18 at 19:23
  • at last I was able to get that desired result ... yes you are correct @ is just hiding \r\n ... I wort that string array to streamwriter by using writeline and that stream passed to third party and its working now ... before it was giving me length exception... thanks all fro your kind help – Muhammad Mansoor Oct 16 '18 at 17:01

2 Answers2

1

If you test your original code by assigning it into a variable:

var value = @"A minErr message has two parts: 
              the message itself and the url that contains the encoded message.
              The message's parameters can contain other error messages which also include error 
              urls.";

Then go to the debugger window and inspect the value variable, you will find this:

"A minErr message has two parts: \r\n  the message itself and the url that contains the encoded message.\r\n  The message's parameters can contain other error messages which also include error urls."

\r and \n are escape sequences:

Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark (").

Approach #1 should work fine. Environment.NewLine represents nonprinting characters:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

So they will not print as \r or \n but rather will be interpreted as a carriage return and line feed operations, respectively.

JuanR
  • 7,405
  • 1
  • 19
  • 30
0

With strings there can be a huge gap between actual data and how it is represented to the user. Including you, the user of a debugger. If you do not know how to interpret a string, you can not get the letters or even figure out where the dang thing ends: https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/

It is not uncommen that the Debugger and Windows/the programm have very different ideas how any given string should be interpreted. Your example: "A minErr message has two parts:/r/n the message itself and the url that contains the encoded message./r/n The message's parameters can contain other error messages which also include error urls." Is just one possible interpreation of what you expect the string to be.

The debugger uses tooltips. Those tooltips do not support multiple lines. This can have technical reasons (tooltips do not support newlines/did not when first written) or useage case reasons (it would make Tolltips hard to read with strings that have a lot of newlines).

/r/n are the "Carriage Return" and "Newline" characters on a Windows. Lacking the ability to actually display the content over multiple line (like a multiline textbox), the Debugger does the next best thing: It displays the newline Escape Characters as part of the string. Any code that actually can display text in multiple lines will properly interpret those characters as what they are supposed to represent.

Christopher
  • 9,634
  • 2
  • 17
  • 31