5

I have the following class as a DataSource for a ListBox:

class SeparatorChars
{
    /// <summary>
    /// Text to describe character
    /// </summary>
    public string Text { get; set; }

    /// <summary>
    /// Char value of the character
    /// </summary>
    public char Value { get; set; }

    /// <summary>
    /// Represent object as string
    /// </summary>
    /// <returns>String representing object</returns>
    public override string ToString()
    {
        return Text + " '" + Value + "'";
    }
}

The problem is, this by default will use the Value just as a regular character added to a string, for example if I define this class for Tab like this:

var TabSeparator = new SeparatorChars {Text = "Tab", Value = '\t'}

The string representation will be:

Tab '     '

But I need it to be

Tab '\t'

How to do this?!

Pratik
  • 11,534
  • 22
  • 69
  • 99
Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • `\t` is the tab character. Looks like you are getting a tab between the `''`. Looks right to me. Am I missing something? Why do you want to output the string `\t`? – Oded Jan 03 '12 at 11:37
  • Thing is, I want it in ToString method to show \t instead of a normal tab. – Dumbo Jan 03 '12 at 11:38
  • Then don't use a tab character but the string `\t`. – Oded Jan 03 '12 at 11:40
  • No I need to use the real character for Value property, since I am going to pass it to another class. If I do for example `var comma = new SeparatorChars {Text = "Comma", Value = ','}` This will be fine. – Dumbo Jan 03 '12 at 11:44
  • @Oliver - That's _two_ characters. – Oded Jan 03 '12 at 11:46
  • 1
    Take a look at this question. The accepted answer may be able to help you. http://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal – Jason Down Jan 03 '12 at 11:47

2 Answers2

6

Admittedly ripped mostly from this post and untested.

public override string ToString()
{
    return ToLiteral(Text + " '" + Value + "'");
}

private string ToLiteral(string input)
{
    var writer = new StringWriter();
    CSharpCodeProvider provider = new CSharpCodeProvider();
    provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
    return writer.ToString();
}
Community
  • 1
  • 1
Jason Down
  • 21,731
  • 12
  • 83
  • 117
  • Hmmm thanks, this works somehow...the representation for tab is now `"Tab \'\t\"'` Seems like everyting is escaped :P – Dumbo Jan 03 '12 at 12:06
  • @Sean87: What if you use it like this: `return ToLiteral(Value.ToString());`? (I'd test it myself, but I'm about to leave for work and on a linux box). – Jason Down Jan 03 '12 at 12:09
  • I think it will be the same thing, since it is going to translted to string automaticly...that will be again `' TAAAAAB! '` instead of '\t' – Dumbo Jan 03 '12 at 12:13
  • OK ok ...I had to use `return Text + " -> " + ToLiteral(Value.ToString()).Replace("\"", "");` :P Thanks! – Dumbo Jan 03 '12 at 12:18
4

Here's a blog post with some sample code: Mark Gu: Escape Sequences in C#

H H
  • 263,252
  • 30
  • 330
  • 514