String Wrapping

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to wrap String Literals in C# without using the ' + ' concatenation operator (or a StringBuilder)

In C++ I could to the following


SomeMethod("This is a really long string that I would want to screen wrap for
"readability reasons. Notice I did not have to perform a string
"concatenation to do this.")


Is there an equivalent in C#

Thanks,
 
Brian said:
Is there a way to wrap String Literals in C# without using the ' + ' concatenation operator (or a StringBuilder)?

In C++ I could to the following:

{
SomeMethod("This is a really long string that I would want to screen wrap for "
"readability reasons. Notice I did not have to perform a string "
"concatenation to do this.");
}

Is there an equivalent in C#?

There doesn't seem to be... I miss that C feature, too.

The closest you'll get in C# is the @"..." literal strings. But, if you
do something like:

{
SomeMethod( @"This is a really long string that I would want
to screen wrap for readability reasons.
Notice I did not have to perform a string
concatenation to do this.");
}

It'll compile, but the string will contain all the newlines and extra
whitespace. This is great for embedding HTML which doesn't care about
the whitespace, but in general it's no good.
 
Brian Reed said:
Is there a way to wrap String Literals in C# without using the ' + '
concatenation operator (or a StringBuilder)?
In C++ I could to the following:

{
SomeMethod("This is a really long string that I would want to screen wrap for "
"readability reasons. Notice I did not have to perform a string "
"concatenation to do this.");
}

Is there an equivalent in C#?

Thanks,

No. However, it should be noted that string literal concatenation is
handled at compile time.

SomeMethod("Hello " + "there")

is compiled into:

SomeMethod("Hello there")

Erik
 
Brian said:
Is there a way to wrap String Literals in C# without using the ' + ' concatenation operator (or a StringBuilder)?

In C++ I could to the following:

{
SomeMethod("This is a really long string that I would want to screen wrap for "
"readability reasons. Notice I did not have to perform a string "
"concatenation to do this.");
}

Is there an equivalent in C#?

Yes, using the '+' operator is the equivalent.

Example:

string s = "one" + "two" + "three";

Compiles to the *exact* same IL code as:

string s = "onetwothree";
 
string str = @"This is a really long string that I would want to screen
wrap for
readability reasons. Notice I did not have to perform a string
concatenation to do this.";

The downside to this format is: on the "following" lines you can't tab in,
because the tabs would become part of the string.


Brian Reed said:
Is there a way to wrap String Literals in C# without using the ' + '
concatenation operator (or a StringBuilder)?
 
Back
Top