Solved

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

Posted on 2011-02-25
14
637 Views
Last Modified: 2013-11-11
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;
0
Comment
Question by:kwh3856
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 34983557
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
 
LVL 12

Expert Comment

by:Amick
ID: 34983560
WebHtmlEditor1.Text = MyGlobalVars.ChiefComplaint + ",\n" + MyGlobalVars.ChiefComplaintNote;
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34983567
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34983578
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34983588
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
 

Author Comment

by:kwh3856
ID: 34985464
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34985532
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34985566
Yeah, what he said  ; )
0
 

Author Comment

by:kwh3856
ID: 34985614
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34985624
It's not "/r/n"....   it's "\r\n" (i.e. backslashes, not forward slashes).
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
ID: 34985652
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
 

Author Comment

by:kwh3856
ID: 34985661
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
 

Author Closing Comment

by:kwh3856
ID: 34985771
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
 

Author Comment

by:kwh3856
ID: 34985773
Here was the website I found it out.  Great website by the way!!!!


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



0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

757 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

19 Experts available now in Live!

Get 1:1 Help Now