用C#替换\\\\ [英] Replacing \n\r in C#

查看:98
本文介绍了用C#替换\\\\的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,当我从包含一些数据的excel中获取内容时,我得到以下值。字符串值包含字符\ n \ r作为新行,我需要替换它们以将其作为新节点放在树视图中。我得到的字符串是



向CDA-ERROR-TAG-DATA(CDA-LEVEL-INDEX)移动''S0500'。 \
\rMOVE FND-ERROR-块SAVE-FND-ERROR-BLOCK.\\\
\rMOVE'S0500-客房服务 '' TO FND-ERROR-TAG-DATA.\\\
\rMOVE LT-PROGRAM -NAME TO FND-PGM-ID.\\\
\rSET FND-APPL-TYPE的实用程序,TRUE.\\\
\rSET FND错误类型,用户对TRUE.\\\
\rMOVE LT-PROGRAM -NAME TO CDA-PROGRAM-ID(CDA-LEVEL-INDEX)。


我需要用字符^替换字符\ n \ r。

我尝试的东西是: -



 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,<跨度类= 代码串> @ <跨度类= 代码串> \\\\
\\ \\ r, ^);
currentNodeSourceText = System.Text.RegularExpressions.Regex.Replace(currentNodeSourceText,<跨度类= 代码串> @ <跨度类= 代码串> \\\ n \\\\, ^);





他们都没有工作。除了这些我尝试了一些,但似乎都是无效的。

请帮助..提前谢谢。

解决方案

你是< b>确定它是\ n \ r?通常在Windows中,行尾字符序列是另一种方式(即\\\\ n)。



此外,你还有4个例子显示有错误的斜杠(即,你有 / n / r 你应该使用 \ n\\\ )。



你做希望字符串的 @ 前缀为这使得斜线字符成为常规字符而不是转义字符序列的部分。



假设原始字符串如图所示,请尝试:

< pre lang =c#> string currentNodeSourceText = MOVE 'S0500' TO CDA-ERROR-TAG-DATA(CDA-LEVEL-INDEX).\\\
\rMOVE FND-ERROR-BLOCK TO SAVE-FND-ERROR-BLOCK.\\\
\rMOVE 'S0500-HOUSEKEEPING' TO FND-ERROR-TAG-DATA.\\\
\rMOVE LT-程序名TO FND-PGM-ID.\\\
\rSET FND-APPL-TYPE的实用程序,TRUE.\\\
\rSET FND -ERROR-TYPE-USER TO TR UE.\\\
n'rMOVE LT-PROGRAM-NAME TO CDA-PROGRAM-ID(CDA-LEVEL-INDEX)。
;

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



正则表达式对于此替换是过度的。 (并且记住替换不修改输入字符串,它返回一个新字符串,所以一定要保存结果。)


< blockquote>检查此链接,看看它是否有效:

how-to-remove-newline-character-or-tab-characters-from-a-net-string [ ^ ]

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 :-

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.

解决方案

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:

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.)


Check this link, see if it works:
how-to-remove-newline-character-or-tab-characters-from-a-net-string[^]


这篇关于用C#替换\\\\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆