15

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";
Mr. Smith
  • 4,288
  • 7
  • 40
  • 82
  • 2
    Create a mapping (i.e. a `Dictionary`) that tells you which character belongs to the other one and then translate character-by-character. – poke Aug 05 '14 at 22:39
  • 1
    @AlexD That page’s formatting is a joke, right? – poke Aug 05 '14 at 22:43
  • Seriously? Or is this a joke? – Darek Aug 05 '14 at 22:47
  • @poke That's certainly one option, but I'm betting there might be an easier way. Perl seems to have a built-in function for this particular conversion. For C#, I'm pondering maybe `NFKC` string normalization? – Mr. Smith Aug 05 '14 at 22:49
  • @AlexD Well, I didn’t comment on the content. And I just don’t get why people even bother publishing such things when it is not only barely readable but the formatting completely breaks the content (the code has lots of syntax errors). – poke Aug 05 '14 at 22:57
  • 1
    @AlexD You might want to post this key point as an answer then? :) – poke Aug 05 '14 at 23:04
  • 1
    Do you mean something like `userInput.Normalize(NormalizationForm.FormKC)`? – petelids Aug 05 '14 at 23:18

1 Answers1

26

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.

petelids
  • 12,305
  • 3
  • 47
  • 57