Solved

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

Posted on 1998-04-23
8
242 Views
Last Modified: 2013-11-19
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?  
0
Comment
Question by:engllf
  • 3
  • 3
  • 2
8 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 20 total points
ID: 1301499
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.
0
 

Author Comment

by:engllf
ID: 1301500
Let me study through your suggestion...
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1301501
look at ww.codeguru.com 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;
myString.Empty();
int n = temp.GetLength();
for (;;) {
  int i = temp.Find('\n');
  if (i < 0) {
    myString += temp;
    break;
  } else {
    myString += temp.Left(i) + "\r\n";
    temp = temp.Mid(i+1);
  }
}

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

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.


0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1301502
Ooops that should have been

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

0
 
LVL 23

Expert Comment

by:chensu
ID: 1301503
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.
0
 

Author Comment

by:engllf
ID: 1301504
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
information.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1301505
You need to balance it. Space or time? The conversion also takes space.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1301506
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

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

829 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