0

I have a WPF app, which is capturing some text as follows:

<TextBox x:Name="MyText" Text="\n,\r"/>

I then pass this to a function which is checking for the presence of these characters in a string:

MyFunc(MyText.Text);

But the characters are being escaped; so MyText.Text looks like this:

"\\n,\\r"

How can I prevent the string from being escaped (if it were a literal, I could simply use the @ character, but this is entered by the user)?

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • Can you clarify: is this text being entered into the TextBox by the user, or is it being set programatically in the XAML? If the latter, then you should be using XML escape sequences, not C#-style escape sequences. The XML (and therefore XAML) equivalent of `\r\n` is ` `. – Mike Strobel Oct 25 '13 at 15:28
  • It is text being entered by the users; but the purpose is to indicate a new line, or other non-visible character – Paul Michaels Oct 25 '13 at 15:32
  • So, is the user entering the literal text `\r\n`, or are they entering a line break? If the former, then you need to unescape the text manually, as in @Sheridan's answer. – Mike Strobel Oct 25 '13 at 15:43
  • No - they are entering literal text, e.g. \n and I'm trying to compare that with an actual line feed. – Paul Michaels Oct 25 '13 at 15:47
  • Then you will need to escape manually. I added an answer showing how. – Mike Strobel Oct 25 '13 at 16:09

5 Answers5

1

How about just replacing \\ with \?:

string unescapedValue = MyText.Text.Replace(@"\\", @"\");

However, I suspect that your text values are just being escaped in Visual Studio Intellisense... what value do you get when you call this:

int actualLength = MyText.Text.Length;

I suspect that your "\\n,\\r" value will return 3 as there are really only 3 characters there. If I'm wrong, just use the first example.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Okay - tried both of these. The problem is that although the third \ is there by the debugger, it shouldn't be. Nor can the escape character be replaced, because, as you've rightly said - it isn't there. – Paul Michaels Oct 25 '13 at 15:03
  • So if it isn't there, then what's problem is this causing? – Sheridan Oct 25 '13 at 15:46
1

Are you sure?
I suspect you are looking at in debug and debug mouse over will escape.

Try

System.Diagnostics.Debug.WriteLine(MyText.Text);
System.Diagnostics.Debug.WriteLine(MyText.Text.Length.ToString());

Length is 5 as \n is two characters not a single control character.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Yes, as replied above; it treats the \n as two characters, so although the escape character isn't REALLY there, it also isn't REALLY a line feed character. – Paul Michaels Oct 25 '13 at 15:31
  • OK but how is that not an answer to the stated question? – paparazzo Oct 25 '13 at 15:36
  • The question was "How can I prevent the string from being escaped?". Your answer was that it isn't being escaped, and it is. – Paul Michaels Oct 25 '13 at 15:39
  • Come on you saw the \\ on the mouse over and got confused. It is not escaped - you are getting what is literally in the text box. You confuse control character with escape. A printer control character has nothing to to with the escape character used in C#. – paparazzo Oct 25 '13 at 16:26
1

You will need to manually unescape the text by detecting escape codes which have themselves been escaped. You can do this like so:

private static string Unescape(string value) {
    if (value == null)
        return null;

    var length = value.Length;
    var result = new StringBuilder(length);

    for (var i = 0; i < length; i++) {
        var c = value[i];

        if (c == '\\' && i++ < length) {
            c = value[i];

            switch (c) {
                case 'n':
                    result.Append('\n');
                    break;
                case 'r':
                    result.Append('\r');
                    break;
                case 't':
                    result.Append('\t');
                    break;
                case '\\':
                    result.Append('\\');
                    break;
                default:
                    result.Append(c);
                    break;
            }
        }
        else {
            result.Append(c);
        }
    }

    return result.ToString();
}
Mike Strobel
  • 25,075
  • 57
  • 69
1

I run into the same problem that you experience, I managed to solve it after reading this post How can I Unescape and Reescape strings in .net?

System.Text.RegularExpressions.Regex.Unescape(MyText.Text);

it will automatically escape your string

\\n => \n
\\t => \t

Extras

i could see a lot of people dont understand how this is causing the problem

a simple example is shown below:

var delimiter = MyText.Text; // this will give you \\t, if you type \t in the textbox
string[] data = new string[] { "hello", "world" };
var concatData = String.Join(text, data); 

// concatData will return => hello\\tworld\\t rather than hello    world
Community
  • 1
  • 1
Tim
  • 3,755
  • 3
  • 36
  • 57
0

You should bind textbox to some property. You can't set formatted Text directly through designer.

Max
  • 156
  • 5