Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Function to Replace Certain UNICODE chars

Posted on 2004-08-11
Medium Priority
Last Modified: 2012-06-21
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?
Question by:hendridm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 24

Expert Comment

ID: 11775160
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;
LVL 24

Expert Comment

ID: 11775167
I meant:
   using Microsoft.VisualBasic;

Author Comment

ID: 11775184
Is there a native C# way to do it?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 24

Accepted Solution

Justin_W earned 1200 total points
ID: 11775339
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);

Author Comment

ID: 11775538
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.

Author Comment

ID: 11775553
nm, I found them here:


Author Comment

ID: 11776088
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();

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question