[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

string format question

Posted on 2016-09-13
15
Medium Priority
?
69 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 29

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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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 64

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 64

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

Industry Leaders: 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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 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