?
Solved

string format question

Posted on 2016-09-13
15
Medium Priority
?
60 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
[X]
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
  • 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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
 
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 1000 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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 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 63

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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