Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am having a code where I am getting the below value when I am fetching something from an excel containing some data. The string value contains the characters \n\r as a new line and I need to replace them to put it in a treeview as a new node. The string which I am getting is

"MOVE 'S0500' TO CDA-ERROR-TAG-DATA(CDA-LEVEL-INDEX).\n\rMOVE FND-ERROR-BLOCK TO SAVE-FND-ERROR-BLOCK.\n\rMOVE 'S0500-HOUSEKEEPING' TO FND-ERROR-TAG-DATA.\n\rMOVE LT-PROGRAM-NAME TO FND-PGM-ID.\n\rSET FND-APPL-TYPE-UTILITY TO TRUE.\n\rSET FND-ERROR-TYPE-USER TO TRUE.\n\rMOVE LT-PROGRAM-NAME TO CDA-PROGRAM-ID(CDA-LEVEL-INDEX)."
I need to replace the character \n\r with a character '^'.
The things which I have tried are :-

C#
System.Text.RegularExpressions.Regex.Replace(currentNodeSourceText,Environment.NewLine, "^");
currentNodeSourceText.Replace("//n/r", "^");
currentNodeSourceText.Replace(@"//n/r", "^");
currentNodeSourceText.Replace(@"///n//r", "^");
currentNodeSourceText.Replace(@"//n/r", "^");
currentNodeSourceText = System.Text.RegularExpressions.Regex.Replace(currentNodeSourceText, @"\\n\r", "^");
currentNodeSourceText = System.Text.RegularExpressions.Regex.Replace(currentNodeSourceText, @"\\\n\\r", "^");


None of them is working. Apart from these I tried few more but all seems to be invalid.
Please help.. Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-13 13:46pm    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

Are you sure it is \n\r? Normally in Windows the end of line character sequence is the other way around (i.e., \r\n).

Also, 4 of the examples you've shown have the wrong slashes (i.e., you have /n/r where you should use \n\r).

You do not want the @ prefix to the strings as that makes the slash characters regular characters instead of parts of escaped character sequences.

Assuming the original string is as shown, try:
C#
string currentNodeSourceText = "MOVE 'S0500' TO CDA-ERROR-TAG-DATA(CDA-LEVEL-INDEX).\n\rMOVE FND-ERROR-BLOCK TO SAVE-FND-ERROR-BLOCK.\n\rMOVE 'S0500-HOUSEKEEPING' TO FND-ERROR-TAG-DATA.\n\rMOVE LT-PROGRAM-NAME TO FND-PGM-ID.\n\rSET FND-APPL-TYPE-UTILITY TO TRUE.\n\rSET FND-ERROR-TYPE-USER TO TRUE.\n\rMOVE LT-PROGRAM-NAME TO CDA-PROGRAM-ID(CDA-LEVEL-INDEX).";

string newText = currentNodeSourceText.Replace("\n\r", "^");

The Regex is overkill for this substitution. (And remember that the Replace does NOT modify the input string, it returns a new string, so be sure to save the result.)
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900