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?!
We can convert a char to a string object in java by using the Character. toString() method.
If you have the ASCII code for a number you can either subtract 30h or mask off the upper four bits and you will be left with the number itself. Likewise you can generate the ASCII code from the number by adding 30h or by ORing with 30h.
The Java comparison method calculates the difference of ASCII values for each character of two strings passed. If the difference comes to “0”, the strings are exactly equal. Otherwise, it prints the difference of ASCII value for the first non-matched character. If the value is negative, the first string is greater.
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();
}
Here's a blog post with some sample code: Mark Gu: Escape Sequences in C#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With