How to replace the \n with \r\n?

I have a string :

      j = 0 ;
      j = sprintf(str, "Test string") ;
      j+= sprintf(str+j, "\nThickness %d ", nThickness);
      j+= sprintf(str+j, "\nWidth %d ", nWidth);

During runtime i wish to replace the \n by \r\n so that i
can display the string properly in an CEdit box.
How do i write simple function to do that?  
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

chensuConnect With a Mentor Commented:
Since we are on the MFC Programming Topic Area, we should use MFC.

CString strBefore;  // your str


CString strAfter;
for (int ni = 0; ni < strBefore.GetLength(); ni++)
    strAfter += strBefore[ni] == '\n' ? "\r\n" : strBefore[ni];

By the way, why can't you use "\r\n" directly instead of "\n"? In Windows, it is recommended using wsprintf rather than sprintf. And, you can use CString::Format.
engllfAuthor Commented:
Let me study through your suggestion...
look at for a better CString extension class that lets you do search and replace.  That might help.

Also, doing this as shown above will be slow as the subscripting and concatenation of the string char by char is not very efficient.

If you want to use CString functionsm, use SpanExcluding or Find to find the '\n' and then insert a '\r' infront. eg.

CString myString; // whatever
CString temp = myString;
int n = temp.GetLength();
for (;;) {
  int i = temp.Find('\n');
  if (i < 0) {
    myString += temp;
  } else {
    myString += temp.Left(i) + "\r\n";
    temp = temp.Mid(i+1);

but if you can, just format with \r\n instead

CString myString;
myString.Format("Test string\nThickness %d \nWidth %d ", nThickness, nWidth);

andsafe yourself the trouble

Please reject and accept my answer if you find it more helpful.

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Ooops that should have been

CString myString;
myString.Format("Test string\r\nThickness %d\r\nWidth %d", nThickness, nWidth);

I don't think the code shown by RONSLOW is faster than mine. RONSLOW's code relies much on how often the '\n' occurs. It will be definitely slower if the '\n' occurs frequently.
engllfAuthor Commented:
About 20% of my string is "\n".
The string is specially formatted and passed to another application via DDE.  The string is analysed and ...

If i am to use "\r\n", my string length
is going to increase significantly containing non-useful
You need to balance it. Space or time? The conversion also takes space.
and you need somewhere to store the resultant string anyway.

The most efficient is to put in the "\r\n" in the first place.  No extra space required for converted string and not time required to convert it.

If that is not practical (for some reason), then my method will probably be faster (if you want to use CString functions).  You can probably get a little bit faster using straight C-like code and the standard library functions - but the code won't look as nice

All Courses

From novice to tech pro — start learning today.