Solved

string format question

Posted on 2016-09-13
15
49 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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