Solved

string format question

Posted on 2016-09-13
15
47 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing page correctly 3 31
Not showing JavaScript in the list 5 40
Entity Framework 7 32
Adware on IIS hosted asp.net website 1 17
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

12 Experts available now in Live!

Get 1:1 Help Now