0

I have a literal as a string in a file

def s_CalculatePartiallyUsedTechPenalty(rate):\n    total = min(rate,0)\n    title = \"Partially Used Technology Penalty\" \n    return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)

when reading the file the text obviously looks like this:

def s_CalculatePartiallyUsedTechPenalty(rate):\\n    total = min(rate,0)\\n    title = \\\"Partially Used Technology Penalty\\\" \\n    return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)

Is there clean way to convert this string so that the value in the file is also the actual value of the string in code. In other words that that \n for example is \n and not \\n.

I understand that I can write a method that goes and replaces all the applicable values, but I do not want to do that unless it is the only way.

Edit: In response to John Wu's answer. No I am not confused. I do understand exactly that this is happening however I want to convert the literal value "\n" to the newline character. So instead of the literal value of \n it should be a new line.

Basically the inverse of How to convert a string containing escape characters to a string

Community
  • 1
  • 1
Murdock
  • 4,352
  • 3
  • 34
  • 63

2 Answers2

0

If you can not fix file-writing code, that you can solve this issues in a following way:

String.Replace(@"\\\", @"\");
String.Replace(@"\\", @"\");

Or, in case, if you normal unescaped string,

String.Replace(@"\\\""", "\"");
String.Replace(@"\\n", Environment.NewLine);

P.s. Also think about other special characters, like \t

UPDATED:

Even better approach:

class Program
{
    static void Main(string[] args)
    {
        var escaped   = @"def s_CalculatePartiallyUsedTechPenalty(rate):\n    total = min(rate,0)\n    title = \""Partially Used Technology Penalty\"" \n    return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)";
        var unescaped = Regex.Unescape(escaped);

        Console.WriteLine(unescaped);
    }
}
Yury Glushkov
  • 711
  • 6
  • 16
  • 1
    To be thorough, he'd probably want to replace [all escape sequences](https://msdn.microsoft.com/en-us/library/ms228362.aspx#Anchor_4). But you're right, he should really fix the code that produces the file. – Mark Waterman Jan 05 '17 at 22:27
0

You are confusing yourself. The string held in the file will be exactly the same as the string held in a string variable obtained by reading the file.

Perhaps you are using Visual Studio to inspect the string (i.e. using the Watch window or just hovering over the variable while the code is in debug mode). In this case, Visual Studio will display the extra slash to indicate that the string variable contains the literal value "\n" instead of a newline character.

If you want to eliminate the escape characters in the Watch window, you can append the format specifier ,nq to the variable name (link).

See also this question on StackOverflow.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80