Solved

string format question

Posted on 2016-09-13
15
45 Views
Last Modified: 2016-09-13
Dear Experts,

I want to simply put each record found on a new line in an email. This is the code from the email block. What do I need to do to add a new line.  I am using C# and visual studio. Thanks. SJ

.
messageBody = "List items";
                        message.Body += " ";

                        List<Items> list = deletedItems.ToList();
                        var testCheck = "{0}, {1}, {2}, {3}, {4}. {5}, {6}, {7}, {8} \n";

                        foreach (var lostItems in list)

                        {
                            message.Body +=
                                  String.Format(testCheck,
                                  lostItems.one, lostItems.two, etc., );
                            
                        }

Open in new window

0
Comment
Question by:sunny-j
  • 4
  • 4
  • 3
  • +3
15 Comments
 
LVL 28

Expert Comment

by:Bill Bach
ID: 41796240
One of these should work for you:
message.Body += "\n"
or
message.Body += "\r\n"
0
 
LVL 1

Expert Comment

by:MotKohn
ID: 41796243
message.Body += "\n";
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41796253
The correct way to do it is to use the Environment.NewLine constant like this:
message.Body += Environment.NewLine;

Open in new window

This ensures proper formatting regardless of localization settings or platform.

https://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
0
 
LVL 26

Expert Comment

by:pony10us
ID: 41796257
I believe "\n" is for Unix platform and "\r\n" is for Windows platform.

Unix only uses the new line while Windows uses Carriage return New Line
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41796274
@pony10us

You are correct. Environment.NewLine properly addresses that.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 41796290
@Russ

I agree. I was simply posting what the difference was.  I had not seen your previous message until I posted mine.  I received a phone call that required my immediate attention so didn't go into more detail. Had I done so I probably would have provided a long if/then option that I would have advised against and then provided the Environment.NewLine however you did a very good job of suggesting so left it at that.  :)

Thank you
1
 

Author Comment

by:sunny-j
ID: 41796396
Dear all, thank you for your swift response. I probably could have composed my question better.
My question is how to create a new line on the list items - I had added in the suggested code as below but this is not working. Is it in the right place?

  

messageBody = "List items";
                        message.Body += " ";

                        List<Items> list = deletedItems.ToList();
                        var testCheck = "{0}, {1}, {2}, {3}, {4}. {5}, {6}, {7}, {8} \n";

                        foreach (var lostItems in list)

                        {
                            message.Body +=
                                  String.Format(testCheck,
                                  lostItems.one, lostItems.two, etc., );
                        message.Body += Environment.NewLine;
                        }
                            

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 20

Expert Comment

by:Russ Suter
ID: 41796404
OK, what does your desired output look like?

According to the code above you want a newline at the end of your comma separated list. The only change I'd make based on what I understand is this:
messageBody = "List items";
                        message.Body += " ";

                        List<Items> list = deletedItems.ToList();
                        var testCheck = "{0}, {1}, {2}, {3}, {4}. {5}, {6}, {7}, {8} " + Environment.NewLine;

                        foreach (var lostItems in list)

                        {
                            message.Body +=
                                  String.Format(testCheck,
                                  lostItems.one, lostItems.two, etc., );
                        message.Body += Environment.NewLine;
                        }

Open in new window

0
 

Author Comment

by:sunny-j
ID: 41796414
it's just one complete line, so lostItems.one, lostItems.two, lostItems.one, lostItems.two, lostItems.one, lostItems.two, lostItems.one, lostItems.two etc., rather than

lostItems.one, lostItems.two
lostItems.one, lostItems.two
lostItems.one, lostItems.two

thanks again
0
 
LVL 26

Accepted Solution

by:
pony10us earned 250 total points
ID: 41796438
I think what you want is:
messageBody = "List items";
                        message.Body += " ";

                        List<Items> list = deletedItems.ToList();
                        var testCheck = "{0}, {1}, {2}, {3}, {4}. {5}, {6}, {7}, {8} " + Environment.NewLine;

                        foreach (var lostItems in list)

                        {
                            message.Body += 
                                  String.Format(testCheck,
                                  lostItems.one, lostItems.two, etc., ) + Environment.NewLine;
                        }

Open in new window

0
 

Author Comment

by:sunny-j
ID: 41796502
no, that didn't work, but it certainly looks like it should - any other suggestions, maybe I need to add to a csv file instead and attach it to an email, thanks again, appreciate your time.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 41796544
Hi sunny-j;

I think the assumption is that the email is in plain text, are you sending the email as HTML body? If so then  to make a line break you need to insert the break tag in the text. Use the <BR > in place of the Environment.NewLine characters. See if that works.
0
 

Author Comment

by:sunny-j
ID: 41796577
Thank you Fernando, that was the issue but as I didn't share that information, I would like to split the points between you and @pony10us given he replied a second time. Thank you all.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41796582
Not a problem sunny-j, glad I was able to help.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 41796663
I appreciate that you found the issue however I think that Russ and Fernando probably deserved more recognition that I.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now