mohaaron said:
Can anyone tell me why this code doesn't work?
Because C# strings are UTF-16, not some random extended ASCII encoding
you've chosen to use.
Hint: if you are casting numeric literals to System.Char, your code is
probably broken.
Both the character you are looking for and the character you want to use
as a replacement should be specified as actual characters. If you don't
want your .cs file to be a UTF-8 encoded file, you can use the \u escape
to specify a character literal using the _Unicode_ UTF-16 code for that
character.
Note that whatever your source extended ASCII encoding is, the character
found at decimal 146 in that encoding is going to have a different
character value in UTF-16. Without knowing exactly what extended ASCII
encoding you are intending to use, I can't say for sure what the actual
UTF-16 value you want is. But based on the code you posted, it appears
you are trying to convert a ’ character (UTF-16 0x2019 to a ' character
(UTF-16 0x27…the same as in ASCII). If so, then your code should look
like this:
Dictionary<char, char> specialChars = new Dictionary<char, char>();
specialChars.Add('’', '\'');
string str = label.Text;
foreach (KeyValuePair<char, char> kvp in specialChars)
{
str = str.Replace(kvp.Key, kvp.Value);
}
label.Text = str;
Note some changes I've made:
– Use actual characters in the call to Add()
– Use the strongly-typed generic Dictionary class
– Defer reassignment of the Text property until all
the processing has been done; assigning the Text
property is much more expensive than just the basic
string operations going on, so avoiding doing it over
and over is a good habit to get into
On the first point, rather than the literals '’' and '\'', if you want
to you could use the \u escape with the actual UTF-16 values: '\u2019'
and '\u0027'.
Note also that if you have a large number of characters that _might_ be
replaced, your proposed solution is very inefficient. You will do much
better scanning the string once, checking each character you find:
Dictionary<char, char> specialChars = new Dictionary<char, char>();
specialChars.Add('’', '\'');
string str = label.Text;
StringBuilder sb = new StringBuilder(str.Length);
foreach (char ch in str)
{
char chAppend;
if (specialChars.TryGetValue(ch, out chAppend))
{
sb.Append(chAppend);
}
else
{
sb.Append(ch);
}
}
label.Text = sb.ToString();
For any input where the string and the list of special characters are
both long, the above should perform better (and for other input, the
performance difference isn't likely to matter).
If you really want to use some other character encoding, such as some
specific extended ASCII encoding, you need to:
– Be specific about which extended ASCII encoding you're using
– Create a byte[] that includes the characters you want to convert to
System.Char
– Use the Encoding class to convert the character data stored in the
byte[] from whatever specific extended ASCII encoding you're using to a
System.String, containing the UTF-16 character instances you want
As you can imagine, doing that is a lot more complicated than just doing
things in UTF-16 in the first place. I'd recommend the simpler approach.
Pete