String Wrapping

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,
 
M

mikeb

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.
 
E

Erik Frey

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
 
C

C# Learner

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";
 
B

Brad Williams

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)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top