Link to home
Start Free TrialLog in
Avatar of Prysson
Prysson

asked on

How do I add a new line in a string

I have a series of strings that I am adding together to create a new string.

For example

string firstname = "firstname"
string lastname = "lastname"
string address = "address"

I want it in a single string with the address on a new line from the first name last name so that the new string if displayed woul look like

firstname lastname
address

How do I do this?

obviously its

string combineadd = firstname + " " + lastname  

I tried adding "\n" but that doesnt work..what is needed to add the address string to the combined string as a new line.


ASKER CERTIFIED SOLUTION
Avatar of Chumad
Chumad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chumad
Chumad

Oops, it might actually be  \r\n for line feed/carriage return for a winapp form :)
Avatar of Dmitry G
For windows applications you may also use Environment.NewLine:

string combineadd = firstname + " " + lastname  + Environment.NewLine + address;

" \r\n" will also work, as Chumad indicated above.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Evilrix, that is good practice to do, however it doesn't fit in this scenario because the string being created is "combineadd" and it's initial creation is being set to firstName + " " + lastName.

If he was doing:

combineadd = firstname;
combineadd += lastname;
combineadd += "\r\n";

Then it would make sense.

It really depends on whether the operator+() results in a new temporary string for each call. The fact that there is only one final assignment in the expression doesn't necessarily indicate only one string instantiation. I'd be only too happy to accept your assertion if you can point me to somewhere in the MSDN that confirms this is indeed not the case.
http://msdn2.microsoft.com/en-us/library/aa691375(VS.71).aspx

The binary + operator performs string concatenation when one or both operands are of type string.

It is therefore reasonable to assume that str0 + str1 is just syntactic sugar for string.Concat(str0, str1), which creates and returns a new string.

http://msdn2.microsoft.com/en-us/library/a6d350wd.aspx

Therefore I conclude that...

string combineadd = firstname + " " + lastname  + Environment.NewLine + address

is really just syntactic sugar for...

string combineadd = string.Concat(firstname, string.Concat(" ", string.Concat(lastname, string.Concat(Environment.NewLine, address))));

...which creates 4 new strings as part of the concatenation process.

Of course I might be wrong but I am unable to find anything to contradict this assumption.

-Rx.
Avatar of Prysson

ASKER

The  was the solution though the suggested answer left off the / in th tag.

But I apprecit the string builder solution..makes builnding the concatenated string much easier.

Thanks
Here is the button;

        private void button4_Click(object sender, EventArgs e)
        {
            string Newline;
            Newline = textFormatting.Newline(TxtIn.Text);
            TxtIn2.Text = Newline;
        }


Here is the class code;

        public static string Newline(string TxtIn)
        {
           return TxtIn + Environment.NewLine;
        }