Skip to content

Extension methods for conversion of Japanese characters

License

Notifications You must be signed in to change notification settings

MyNihongo/KanaConverter

Repository files navigation

Version NuGet downloads
Extension methods for kana conversion.

  • ToRomaji() - convert Hiragana (ひらがな) or Katakana (カタカナ) to romaji.
  • ToHiragana() - convert romaji to Hiragana (ひらがな).
  • ToKatakana() - convert romaji to Katakana (カタカナ).
  • KanaToHiragana() - convert Hiragana (ひらがな) or Katakana (カタカナ) to Hiragana (ひらがな).
  • KanaToKatakana() - convert Hiragana (ひらがな) or Katakana (カタカナ) to Katakana (カタカナ).
var romaji = "ひらがな・カタカナ".ToRomaji(); // result "hiraganakatakana"
var hiragana = "hiragana".ToHiragana(); // result "ひらがな"
var katakana = "katakana".ToKatakana(); // result "カタカナ"
var kanaToHiragana = "ひらがな・カタカナ".KanaToHiragana(UnrecognisedCharacterPolicy.Append); // result "ひらがな・かたかな"
var kanaToKatakana = "ひらがな・カタカナ".KanaToKatakana(UnrecognisedCharacterPolicy.Append); // result "ヒラガナ・カタカナ"

StringBuilder with Microsoft.Extensions.ObjectPool

using Microsoft.Extensions.ObjectPool;

var stringBuilderPool = new DefaultObjectPoolProvider()
	.CreateStringBuilderPool();

for (var i = 0; i < 1_000_000; i++)
{
	var romaji = "ひらがな・カタカナ".ToRomaji(stringBuilderPool: stringBuilderPool);
}

Handling unrecognised characters

When an unexpected character is encountered in the supplied character sequence the following options are available:

  • ThrowException (default): InvalidCharacterException will be thrown.
  • Skip: characters will not be processed (just ignored).
  • Append: characters will be added to the result string (appended).