C #: How do you construct a multi-line string at design time?

How would I be able to display a string like below in a console window by writing it to a variable at design time and then just calling Console.WriteLine (sDescription) to display it?

        Options:
            -t          Description of -t argument.
            -b          Description of -b argument.

      

0


source to share


6 answers


If I understand your question correctly, you need the @ sign in front of your line. This will force the compiler to take your string literally (including newlines, etc.)

In your case, I would write the following:

String sDescription =
@"Options:
    -t          Description of -t argument.";

      



So far for your question (hopefully), but I would suggest just using multiple WriteLines. The performance penalty costs next to nothing, and it's just more adaptable. You can work with a format string so you can do this:

string formatString = "{0:10} {1}";
Console.WriteLine("Options:");
Console.WriteLine(formatString, "-t", "Description of -t argument.");
Console.WriteLine(formatString, "-b", "Description of -b argument.");

      

formatstring makes sure your strings are formatted nicely without manually putting spaces in, and make sure that if you ever want to change the format, you just need to do it in one place.

+4


source


Console.Write("Options:\n\tSomething\t\tElse");

      

produces



Options:
    Something       Else

      

\ n for next line, \ t for tab, for more professional layouts try setting margin width using format specifiers. http://msdn.microsoft.com/en-us/library/txafckwd.aspx

+2


source


If it's a screen /?

, I tend to drop text into a .txt file that I paste via the resx file. Then I just edit the txt file. It then opens up as a string property in the generated resx class.

I insert standard characters string.Format

into my replacement txt as needed.

+2


source


Personally, I usually just write three calls to Console.WriteLine. I know this gives extra fluff, but it aligns the text appropriately and makes sure it uses the correct line terminator for whatever platform I'm running on. An alternative would be to use a shorthand literal, but this would "fix" the line terminator at compile time.

+1


source


I know C # is mostly used on Windows machines, but please please try to write your code as platform neutral. Not all platforms have the same end-of-line character. To get the line end character correctly for the current executable platform, you should use:

System.Environment.NewLine

      

Maybe I'm just anal because I'm a former Java programmer who has run applications on many platforms, but you never know what the platform of the future is.

+1


source


The "best" answer depends on where the information you are showing comes from.

If you want to hard-code it, using the "@" line is very efficient, although you will find that when displayed on the right it plays a hilarious code-formatted hell.

For a more substantial piece of text (more than two lines) a good use of text resources.

But if you need to build a string on the fly, say by navigating the command line options supported by your application, then you should examine both StringBuilder and Format Strings.

StringBuilder has methods such as AppendFormat () that accept format strings, making it easier to create format strings.

Format strings make it easy to combine multiple elements together. Note that formatted strings can be used to format objects to a specific width.

To quote the MSDN page linked above:

Format Element Syntax

Each format element takes the following forms and consists of the following components:

{index [, alignment] [: FormatString]}

Matching curly braces ("{" and "}") are required.

Index component

The required component of the index, also called the parameter specifier, is a number starting with 0 that identifies the corresponding element in the object list ...

Alignment component

The optional alignment component is an integer indicating the preferred formatted field width. If the alignment value is less than the formatted string length, the alignment is ignored, and the formatted string length is used as the field width. Formatted data in a field is right-aligned if the alignment is positive and left-aligned if the alignment is negative. A space is used if padding is required. a comma is required if alignment is specified.

String Formatting Component

The optional formatString component the appropriate format string for the type of object being formatted ...

0


source







All Articles