• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1690
  • Last Modified:

Function to Replace Certain UNICODE chars

I need a function to replace the unicode quotes from a string to standard quotes (") and also unicode single quotes.  Here is how I might have done it in ASP:

' convert all types of single quotes
tmpString = replace(tmpString, chr(145), chr(39))
tmpString = replace(tmpString, chr(146), chr(39))

' convert all types of double quotes
tmpString = replace(tmpString, chr(147), chr(34))
tmpString = replace(tmpString, chr(148), chr(34))

How can I do this in C#/ASP.NET?
  • 4
  • 3
1 Solution
Since C# is case-sensitive, use this:
   ' convert all types of single quotes
   tmpString = tmpString.Replace(Chr(145), Chr(39));
   tmpString = tmpString.Replace(Chr(146), Chr(39));

   ' convert all types of double quotes
   tmpString = tmpString.Replace(Chr(147), Chr(34));
   tmpString = tmpString.Replace(Chr(148), Chr(34));

Note that since the Chr function is a VB function, you might have to also include something like this:
   using Mucrosoft.VisualBasic;
I meant:
   using Microsoft.VisualBasic;
hendridmAuthor Commented:
Is there a native C# way to do it?
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Sure.  You can declare Char variables like this:
   char MyChar = 'X';        // Character literal
   char MyChar = '\x0058';   // Hexadecimal
   char MyChar = (char)88;   // Cast from integral type
   char MyChar = '\u0058';   // Unicode

E.g., if your Char variables are called c34, c39, and c145-c148, then do:
   ' convert all types of single quotes
   tmpString = tmpString.Replace(c145, c39);
   tmpString = tmpString.Replace(c146, c39);

   ' convert all types of double quotes
   tmpString = tmpString.Replace(c147, c34);
   tmpString = tmpString.Replace(c148, c34);
hendridmAuthor Commented:
I can't figure out the code for “ and ”.

I tried \u0147 but it displays some funky character.

Where can I find the codes?  I tried Google w/o success.
hendridmAuthor Commented:
nm, I found them here:

hendridmAuthor Commented:
For the record, in case someone else stumbles across this PAQ in the future, here is eventually what I used:

    private string StipUnicode(string s)
      // convert all types of single quotes
      s=s.Replace("\u2018", "&#39;"); // left single quotation mark
      s=s.Replace("\u2019", "&#39;"); // right single quotation mark
      s=s.Replace("'", "&#39;"); // single quotes

      // convert all types of double quotes
      s=s.Replace("\u201C", "&quot;"); // left double quotation mark
      s=s.Replace("\u201D", "&quot;"); // right double quotation mark
      //replace carriage returns & line feeds
      s=s.Replace("\n", "");
      s=s.Replace("\r", "");
      return s.Trim();
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now