Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I add a new line in a string

Posted on 2007-12-03
9
409 Views
Last Modified: 2012-06-27
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.


0
Comment
Question by:Prysson
9 Comments
 
LVL 8

Accepted Solution

by:
Chumad earned 300 total points
ID: 20398061
Is this supposed to be displayed on a webpage? If so, use "<br>" as a line break.  If it's a winapp, you may also try /n/r
0
 
LVL 8

Expert Comment

by:Chumad
ID: 20398070
Oops, it might actually be  \r\n for line feed/carriage return for a winapp form :)
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 20398273
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
ID: 20398727
You may want to consider this: -

"A String object is called immutable (read-only) because its value cannot be modified once it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class."

http://msdn2.microsoft.com/en-us/library/system.string.aspx
http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.aspx
            StringBuilder sb = new StringBuilder();
            sb.Append(firstname);
            sb.Append(" ");
            sb.Append(lastname);
            sb.Append(Environment.NewLine);
            sb.Append(address);
 
            string s = sb.ToString();

Open in new window

0
 
LVL 8

Expert Comment

by:Chumad
ID: 20398810
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.

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20398876
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.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399003
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.
0
 

Author Closing Comment

by:Prysson
ID: 31412412
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
0
 

Expert Comment

by:mhouldridge
ID: 20640357
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;
        }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 41
Hey Audio! 9 44
Need help with a Crystal Report in my C#.Net application 2 26
length of the password hash sha1:64000 to set sql field property. 13 66
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question