开发者

Placing NewLines Between Strings in .NET

i am sending email through my application using the following code of c#

 myMail.Body = TextBox1.Text+
                      txtName.Text+
                      txtCName.Text+
                      txtAddress.Text+
                      TextBox1.Text+
                      txtCity.Text+
                      txtState.Text+
                      txtCountry.Text+
                      txtPhone.Text+
                      Fax.Text+
                      txtCell.Text+
                      txtEmail.Text+
               开发者_Go百科       txtPrinting.Text;
        myMail.BodyEncoding = System.Text.Encoding.UTF8;

but i am getting mail in this form "sheerazahmedShehzoreHyderabadsheerazHyderabadSindhPakistan03453594552034598750258741sheery_1@hotmail.comsingle" i.e merging all values, i want each value of textboxt in a separate new line i.e

Sheeraz Ahmed
Shehzore
Hyderabad 

etc.


StringBuilder sb = new StringBuilder();

sb.AppendLine(TextBox1.Text);
sb.AppendLine(txtName.Text);
...


myMail.Body = sb.ToString();


myMail.Body = TextBox1.Text + Environment.NewLine + 
                  txtName.Text+ Environment.NewLine + 
                  txtCName.Text+ Environment.NewLine + 
                  txtAddress.Text+ Environment.NewLine + 
                  TextBox1.Text+ Environment.NewLine + 
                  txtCity.Text+ Environment.NewLine + 
                  txtState.Text+ Environment.NewLine + 
                  txtCountry.Text+ Environment.NewLine + 
                  txtPhone.Text+ Environment.NewLine + 
                  Fax.Text+ Environment.NewLine + 
                  txtCell.Text+ Environment.NewLine + 
                  txtEmail.Text+ Environment.NewLine + 
                  txtPrinting.Text;
    myMail.BodyEncoding = System.Text.Encoding.UTF8;

Or better yet, use a stringbuilder or string.Format

StringBuilder bodyBuilder = new StringBuilder("");
bodyBuilder .AppendLine(TextBox1.Text);
bodyBuilder .AppendLine(txtName.Text);
bodyBuilder .AppendLine(txtCName.Text);
bodyBuilder .AppendLine(txtAddress.Text);
  // etc.
myMail.Body = bodyBuilder .ToString();

or

myMail.Body = String.Format("{0}{1}{2}{1}{3}{1} ... ", TextBox1.Text, Environment.NewLine, txtCName.Text, txtAddress.Text -- etc...


myMail.Body = TextBox1.Text+ Environment.NewLine +
                      txtName.Text+ Environment.NewLine +
                      txtCName.Text+ Environment.NewLine +
                      txtAddress.Text+ Environment.NewLine +
                      TextBox1.Text+ Environment.NewLine +
                      txtCity.Text+ Environment.NewLine +
                      txtState.Text+ Environment.NewLine +
                      txtCountry.Text+ Environment.NewLine +
                      txtPhone.Text+ Environment.NewLine +
                      Fax.Text+ Environment.NewLine +
                      txtCell.Text+ Environment.NewLine +
                      txtEmail.Text+ Environment.NewLine +
                      txtPrinting.Text;


Use String.Format in combination with Environment.NewLine.

e.g.

String.Format("{0}{1}{2}{1}{3}", "ValueOfTextbox1", Environment.NewLine, "ValueOfTextbox2", "ValueOfTextbox3");

You only have to define one placeholder in the string for Environment.NewLine and you can reuse it.


result = text1 + Environment.NewLine + text2 + Environment.NewLine + text3;

this will put text1,text2 and text3 in a separate line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜