Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert fullwidth to halfwidth

Tags:

c#

unicode

In C#, how do I convert a string that's using fullwidth form characters into halfwidth form characters?

For example, given userInput below, I want to convert Stackoverflow to Stackoverflow:

string userInput= "Stackoverflow";
//string userInput= "Stackoverflow";
like image 606
Mr. Smith Avatar asked Aug 05 '14 22:08

Mr. Smith


1 Answers

You can use the string.Normalize() method:

string userInput = "Stackoverflow";
string result = userInput.Normalize(NormalizationForm.FormKC);
//result = "Stackoverflow"

See example on DotNetFiddle.

More information on the Normalization Forms can be found on unicode.org.

like image 113
petelids Avatar answered Oct 23 '22 01:10

petelids