How do you add a carriage return to a string of text stored in a variable?

I am trying to creat a long string concatenated by a string of variables.  My problem is I would like to put a carriage return after each concatenated variable.  How do I do this?  Here is an example of my strings

WebHtmlEditor1.Text = MyGlobalVars.ChiefComplaint + ", " + MyGlobalVars.ChiefComplaintNote;
kwh3856Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
käµfm³d 👽Connect With a Mentor Commented:
You have a couple of options:
WebHtmlEditor1.Text = MyGlobalVars.ChiefComplaint + ", " + MyGlobalVars.ChiefComplaintNote + "\r" + MyGlobalVars.SomeOther + "\r";

--OR--

WebHtmlEditor1.Text = string.Format("{0}\r{1}\r{2}", MyGlobalVars.ChiefComplaint, MyGlobalVars.ChiefComplaintNote, MyGlobalVars.SomeOther);

Open in new window

0
 
AmickCommented:
WebHtmlEditor1.Text = MyGlobalVars.ChiefComplaint + ",\n" + MyGlobalVars.ChiefComplaintNote;
0
 
Todd GerbertIT ConsultantCommented:
Have a look at the String.Format() method (http://msdn.microsoft.com/en-us/library/system.string.format.aspx) and the StringBuilder class (http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx), both are better options than concatenating strings with the + operator.

But, to answer your question in C# you can put a carriage return/linefeed pair directly into a string with \r\n, so:

WebHtmlEditor1.Text = MyGlobalVars.ChiefComplaint + "\r\n " + MyGlobalVars.ChiefComplaintNote + "\r\n" + someOtherVariable;

You can also use Environment.NewLine (which is always equal to "\r\n" on a Windows system, but is technically more portable):

WebHtmlEditor1.Text = MyGlobalVars.ChiefComplaint + Environment.NewLine + MyGlobalVars.ChiefComplaintNote + Environment.NewLine + "," + someOtherVariable;

But I recommend using StringBuilder:
StringBuilder sb = new StringBuilder();
sb.AppendLine(MyGlobalCars.ChiefComplaint); // Appends variable plus a new line
sb.Append(someOtherVar); // Appends variable without a new line
WebHtmlEditor1.Text = sb.ToString();

Or String.Format:
WebHtmlEditor1.Text = String.Format("{0}\r\n{1}\r\n{2}", MyGlobalVars.ChiefComplaint, MyGlobalVars.ChiefComplaintNote, someOtherVar);
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
käµfm³d 👽Commented:
If you had your variables in a list, you could do something like this:
List<string> varList = new List<string>();

.. Add variable values to list ..

foreach (string varValue in varList)
{
    WebHtmlEditor1.Text += varValue + "\r";
}

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
I should point out that "\r" is a carriage return, and "\n" is a newline.  Normally, in Windows, the end-of-line sequence is \r\n, but if you have some specific need for just a carriage return use \r by itself.
0
 
kwh3856Author Commented:
Kaufmed,
I tried your code and here is the output.  Is there something I am doing wrong?

, neck pain/ cervical radiculopathy 46 yr old male Post OP F/UP per dr smith c/o neck pain radiate to bilat shoulders Lunesta 3 mg Tab, Rx Strength = 3, Take = 3, Frequency = , Duration = , Dispense = , Refills = 1 Allegra D, Rx Strength = 60 mg, Take = 1 tab, Frequency = bid, Duration = 30 days, Dispense = 60, Refills = Clarinex, Rx Strength = 5 mg, Take = 1 tab(s), Frequency = once a day, Duration = 30 day(s), Dispense = 30, Refills = Vioxx, Rx Strength = 25 mg, Take = 1 tab, Frequency = qd, Duration = , Dispense = , Refills = lipitor, Rx Strength = 10mg, Take = as directed, Frequency = daily, Duration = , Dispense = , Refills =


var ChiefCompliantNoteResult = from chiefcomplaintnotedata in dcMed.SOAPnotes
                                               where chiefcomplaintnotedata.CREncounterId == CRguid
                                               select new { chiefcomplaintnotedata };


                foreach (var chiefcomplaintnoterec in ChiefCompliantNoteResult)
                {
                    MyGlobalVars.ChiefComplaintNote = chiefcomplaintnoterec.chiefcomplaintnotedata.ChiefComplaint;
                   
                    RichTextBox rtb = new RichTextBox();
                    rtb.Rtf = MyGlobalVars.ChiefComplaintNote;

                    MyGlobalVars.ChiefComplaintNote = rtb.Text;
  
                
                }

                MyGlobalVars.RxCount = 0;

                var RXresult = from rxdata in dcMed.RXes
                               where rxdata.CRencounterId == CRguid
                               select new { rxdata };

                int rxcount = RXresult.Count();
                MyGlobalVars.RxCount = rxcount;
                string[] rxarray = new string[rxcount];

                int i = 0;

                foreach (var rsdatarec in RXresult)
                {
                    
                        rxarray[i] = rsdatarec.rxdata.RxItemName + ", Rx Strength = " + rsdatarec.rxdata.RxStrength + ", Take = " + rsdatarec.rxdata.RxTakeValue + ", Frequency = " + rsdatarec.rxdata.RxFreq + ", Duration = " + rsdatarec.rxdata.RxDuration + ", Dispense = " + rsdatarec.rxdata.RxDespenseValue + ", Refills = " + rsdatarec.rxdata.RxRefillsValue;

                        MyGlobalVars.Rx[i] = rxarray[i];

                        i++;
                    
                }
                
            }



            // Concantenate Rx Array
            int rxindex;
            for (rxindex = 0; rxindex < MyGlobalVars.RxCount; rxindex++)
            {
                MyGlobalVars.RxConCat = string.Format("{0}\r{1}\r",MyGlobalVars.RxConCat, MyGlobalVars.Rx[rxindex]);
            }


            WebHtmlEditor1.Text = string.Format("{0}\r{1}\r{2}\r",MyGlobalVars.ChiefComplaint, MyGlobalVars.ChiefComplaintNote, MyGlobalVars.RxConCat);


        }

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
You used a carriage return by itself, which doesn't create a new line of text.  You need a carriage return linefeed pair - that is change your \r to \r\n.

 // Concantenate Rx Array
            int rxindex;
            for (rxindex = 0; rxindex < MyGlobalVars.RxCount; rxindex++)
            {
                MyGlobalVars.RxConCat = string.Format("{0}\r\n{1}\r\n",MyGlobalVars.RxConCat, MyGlobalVars.Rx[rxindex]);
            }

Open in new window

(Incidentally, I'm not sure this code block does what you intended - MyGlobalVars.RxConCat will only ever be equal to the value set on the last iteration of the loop)

WebHtmlEditor1.Text = string.Format("{0}\r\n{1}\r\n{2}\r\n",MyGlobalVars.ChiefComplaint, MyGlobalVars.ChiefComplaintNote, MyGlobalVars.RxConCat);

Open in new window

0
 
käµfm³d 👽Commented:
Yeah, what he said  ; )
0
 
kwh3856Author Commented:
still no luck.  When I debugged the code I could see it putting the /r/n in the string but when it was displayed in the HTML editor I got the same output as above.  Next, I deleted the HTML editor and desided to use a plain old text box and then I got one single line of text that I had to scroll to the right to see all the data.  Is the problem the object that I am using to try to display the data?  What kind of object should I be using to interpret the /r/n codes?
0
 
käµfm³d 👽Commented:
It's not "/r/n"....   it's "\r\n" (i.e. backslashes, not forward slashes).
0
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
Sorry, I didn't catch that you were using an HTML editor...

In HTML whitespace (like spaces, tabs, and carriage return/linefeeds) is ignored.  To get a newline in HTML you need a <br /> tag.  I usually leave \r\n in the text, in addition to the <br /> - the <br /> causes the newline in the HTML, but if I want to view the source the \r\n make it easier to read.  The exact behavior of the particular HTML editor you're using might be different...

 // Concantenate Rx Array
            int rxindex;
            for (rxindex = 0; rxindex < MyGlobalVars.RxCount; rxindex++)
            {
                MyGlobalVars.RxConCat = string.Format("{0}<br />\r\n{1}<br />\r\n",MyGlobalVars.RxConCat, MyGlobalVars.Rx[rxindex]);
            }

Open in new window

0
 
kwh3856Author Commented:
Here is my code.  Still no luck.
// Concantenate Rx Array
            int rxindex;
            for (rxindex = 0; rxindex < MyGlobalVars.RxCount; rxindex++)
            {
                MyGlobalVars.RxConCat = MyGlobalVars.RxConCat + string.Format("{0}\r\n{1}\r\n", MyGlobalVars.RxConCat, MyGlobalVars.Rx[rxindex]);
            }


            wtSOAP.Text = string.Format("{0}\r\n{1}\r\n{2}\r\n",MyGlobalVars.ChiefComplaint, MyGlobalVars.ChiefComplaintNote, MyGlobalVars.RxConCat);

Open in new window

0
 
kwh3856Author Commented:
tgerbert,
That was it.  I finally found it at www3schools.  I went back to post up what I found and you had already posted the correct answer.  Thanks to each of you who educated me in the way of formatting text.
0
 
kwh3856Author Commented:
Here was the website I found it out.  Great website by the way!!!!


http://www.w3schools.com/tags/tag_br.asp



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.