I am working on writing code to clear user input on my ASP.NET site. I need to clear the input to remove all references to the ASCII characters 145, 146, 147, 148, which are sometimes entered by my Mac users who copy and paste the contents that they write in the word processor on their poppies.
My problem is the following three lines, which I suppose should output the same text.
string test1 = Convert.ToChar(147).ToString(); string test2 = String.Format("'{0}'", Convert.ToChar(147)); char[] characters = System.Text.Encoding.ASCII.GetChars(new byte[] { 147 }); string test3 = new string(characters);
However, when I set the ASP TextBox equal to the following
txtShowValues.Text = test1 + "*" + test2 + "*" + test3;
I get an empty value for test1, test2 works correctly, and test3 produces as "?".
Can someone explain what is going on differently. I hope this helps me understand how .NET uses ASCII values for characters greater than 128 so that I can write a good cleanup script.
EDIT
The values I mentioned (145 - 148) are curly quotes. Thus, one left, one right, double left, double right.
By “working correctly” I mean that it displays an italic quote in my browser.
SECOND EDIT
The following code (mentioned in the answer) also displays curly quotes. Therefore, perhaps the problem was using ASCII in test 3.
char[] characters2 = System.Text.Encoding.Default.GetChars(new byte[] { 147 }); string test4 = new string(characters2);
THIRD CHANGE
I found a mac that I could lend, and was able to repeat the problem. When I copy and paste text that contains quotes from Word into my web application on mac, it inserts curly quotes (147 and 148). When I click, saving italic quotes is stored in the database, so I will use the code that you helped me to brighten up this content.
FOUTH EDIT
Spent some time writing another sample code based on the answers here and noticed that it has something to do with multi-threaded text blocks in ASP.NET. There was good information here, so I decided to just ask a new question: ASP.NET A multi-line text box allowing input over UTF-8