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

13
  • #1 should have worked fine. What makes you think it didnt Commented Oct 15, 2018 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." Commented Oct 15, 2018 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. Commented Oct 15, 2018 at 18:52
  • 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. Commented Oct 15, 2018 at 19:15
  • 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. Commented Oct 15, 2018 at 19:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.