Java/JS 日语全半角,平片假名转换

方式1: 安装icu4

官方Github

https://github.com/unicode-org/icu

import com.ibm.icu.text.Transliterator;

⏹全角转换半角
Transliterator literator1 = Transliterator.getInstance("Fullwidth-Halfwidth");
literator1.transliterate("カナ");  // カナ
  
⏹半角转换全角
Transliterator literator2 = Transliterator.getInstance("Halfwidth-Fullwidth");
literator2.transliterate("カナ");  // カナ

⏹平假名转换片假名
Transliterator literator4 = Transliterator.getInstance("Hiragana-Katakana");
literator4.transliterate("なし");  // ナシ

⏹片假名转换平假名
Transliterator literator3 = Transliterator.getInstance("Katakana-Hiragana");
literator3.transliterate("カナ");  // かな

依赖

<dependency>
        <groupId>com.ibm.icugroupId>
        <artifactId>icu4jartifactId>
        
        <version>3.4.4version>
dependency>

方式2

参考 https://github.com/shogo4405/KanaXS
以下js代码均摘录于KanaXS中,该项目中还包含Java和C#的转换

2.1 全角英数字を半角英数字に変換します

/**
 * 全角英数字を半角英数字に変換します。
 * @example
 * "abcABC123".toHankakuCase(); // abcABC123
 * @return {String}
 */
String.prototype.toHankakuCase = function()
{
	var c, i = this.length, a = [];

	while(i--)
	{
		c = a[i] = this.charCodeAt(i);

		switch(true)
		{
			// 【!】~【~】の範囲
			case (0xFF01 <= c && c <= 0xFF5E):
				a[i] -= 0xFEE0;
				break;
			// 全角スペース
			case (c == 0x3000):
				a[i] = 0x0020;
				break;
		};
	};


	return String.fromCharCode.apply(null, a);
};

2.2 全角のカタカナを半角のカタカナに変換します

/**
 * 全角のカタカナを半角のカタカナに変換します。
 * @example
 * "アイウエオ".toHankanaCase(); // アイウエオ
 * @return 
 */
String.prototype.toHankanaCase = function()
{
	var i, f, c, a = [], m = String.prototype.toHankanaCase.MAPPING;

	for(i = 0, f = this.length;i < f;)
	{
		c = this.charCodeAt(i++);
		switch(true)
		{
			case (c in m):
				a.push(m[c]);
				break;
			case (0x30AB <= c && c <= 0x30C9):
				a.push(m[c - 1], 0xFF9E);
				break;
			case (0x30CF <= c && c <= 0x30DD):
				a.push(m[c - c % 3], [0xFF9E,0xFF9F][c % 3 - 1]);
				break;
			default:
				a.push(c);
				break;
		};
	};
	
	// 那个日本人Github中的代码有bug,当全角片假名为ジャ、ジュ、ジョ等假名时ャ,ュ,ョ无法转换为半角片假名
	// 暂且添加replaceAll("ャ", "ャ").replaceAll("ュ", "ュ").replaceAll("ョ", "ョ") 来应付需求
	return String.fromCharCode.apply(null, a).replaceAll("ャ", "ャ").replaceAll("ュ", "ュ").replaceAll("ョ", "ョ");
};

String.prototype.toHankanaCase.MAPPING =
{
	0x30A1:0xFF67,
	0x30A3:0xFF68,
	0x30A5:0xFF69,
	0x30A7:0xFF6A,
	0x30A9:0xFF6B,
	0x30FC:0xFF70,
	0x30A2:0xFF71,
	0x30A4:0xFF72,
	0x30A6:0xFF73,
	0x30A8:0xFF74,
	0x30AA:0xFF75,
	0x30AB:0xFF76,
	0x30AD:0xFF77,
	0x30AF:0xFF78,
	0x30B1:0xFF79,
	0x30B3:0xFF7A,
	0x30B5:0xFF7B,
	0x30B7:0xFF7C,
	0x30B9:0xFF7D,
	0x30BB:0xFF7E,
	0x30BD:0xFF7F,
	0x30BF:0xFF80,
	0x30C1:0xFF81,
	0x30C4:0xFF82,
	0x30C6:0xFF83,
	0x30C8:0xFF84,
	0x30CA:0xFF85,
	0x30CB:0xFF86,
	0x30CC:0xFF87,
	0x30CD:0xFF88,
	0x30CE:0xFF89,
	0x30CF:0xFF8A,
	0x30D2:0xFF8B,
	0x30D5:0xFF8C,
	0x30D8:0xFF8D,
	0x30DB:0xFF8E,
	0x30DE:0xFF8F,
	0x30DF:0xFF90,
	0x30E0:0xFF91,
	0x30E1:0xFF92,
	0x30E2:0xFF93,
	0x30E4:0xFF94,
	0x30E6:0xFF95,
	0x30E8:0xFF96,
	0x30E9:0xFF97,
	0x30EA:0xFF98,
	0x30EB:0xFF99,
	0x30EC:0xFF9A,
	0x30ED:0xFF9B,
	0x30EF:0xFF9C,
	0x30F2:0xFF66,
	0x30F3:0xFF9D,
	0x30C3:0xFF6F
};

2.3 全角カタカナを全角ひらがに変換します

/**
 * 全角カタカナを全角ひらがに変換します。
 * @example
 * "アイウエオ".toHiraganaCase(); // あいうえお
 * @return {String}
 */
String.prototype.toHiraganaCase = function()
{
	var c, i = this.length, a = [];

	while(i--)
	{
		c = this.charCodeAt(i);
		a[i] = (0x30A1 <= c && c <= 0x30F6) ? c - 0x0060 : c;
	};

	return String.fromCharCode.apply(null, a);
};

2.4 全角ひらがなを全角カタカナに変換します。

/**
 * 全角ひらがなを全角カタカナに変換します。
 * @example
 * "あいうえお".toKatakanaCase(); // アイウエオ
 * @return {String}
 */
String.prototype.toKatakanaCase = function()
{
	var c, i = this.length, a = [];

	while(i--)
	{
		c = this.charCodeAt(i);
		a[i] = (0x3041 <= c && c <= 0x3096) ? c + 0x0060 : c;
	};

	return String.fromCharCode.apply(null, a);
};

2.5 半角英数字を全角英数字に変換します。

/**
 * 半角英数字を全角英数字に変換します。
 * @example
 * "abcABC123".toZenkakuCase(); // abcABC123
 * @return {String}
 */
String.prototype.toZenkakuCase = function()
{
	var c, i = this.length, a = [];

	while(i--)
	{
		c = a[i] = this.charCodeAt(i);
		switch(true)
		{
			case (c <= 0x007E && 0x0021 <= c):
				a[i] += 0xFEE0;
				break;
			case (c == 0x0020):
				a[i] = 0x3000;
				break;
		};
	};

	return String.fromCharCode.apply(null, a);
};

2.6 半角のカタカナを全角のカタカナに変換します。

/**
 * 半角のカタカナを全角のカタカナに変換します。
 * @example
 * "アイウエオ".toZenkanaCase(); // アイウエオ
 * @return {String}
 */
String.prototype.toZenkanaCase = function()
{
	var i, f, c, a = [], m = String.prototype.toZenkanaCase.MAPPING;

	for(i = 0, f = this.length;i < f; i++)
	{
		c = this.charCodeAt(i);
		a.push(m[c] || c);
	};

	return String.fromCharCode.apply(null, a);
};

String.prototype.toZenkanaCase.MAPPING =
{
		0xFF67:0x30A1,
		0xFF68:0x30A3,
		0xFF69:0x30A5,
		0xFF6A:0x30A7,
		0xFF6B:0x30A9,
		0xFF70:0x30FC,
		0xFF71:0x30A2,
		0xFF72:0x30A4,
		0xFF73:0x30A6,
		0xFF74:0x30A8,
		0xFF75:0x30AA,
		0xFF76:0x30AB,
		0xFF77:0x30AD,
		0xFF78:0x30AF,
		0xFF79:0x30B1,
		0xFF7A:0x30B3,
		0xFF7B:0x30B5,
		0xFF7C:0x30B7,
		0xFF7D:0x30B9,
		0xFF7E:0x30BB,
		0xFF7F:0x30BD,
		0xFF80:0x30BF,
		0xFF81:0x30C1,
		0xFF82:0x30C4,
		0xFF83:0x30C6,
		0xFF84:0x30C8,
		0xFF85:0x30CA,
		0xFF86:0x30CB,
		0xFF87:0x30CC,
		0xFF88:0x30CD,
		0xFF89:0x30CE,
		0xFF8A:0x30CF,
		0xFF8B:0x30D2,
		0xFF8C:0x30D5,
		0xFF8D:0x30D8,
		0xFF8E:0x30DB,
		0xFF8F:0x30DE,
		0xFF90:0x30DF,
		0xFF91:0x30E0,
		0xFF92:0x30E1,
		0xFF93:0x30E2,
		0xFF94:0x30E4,
		0xFF95:0x30E6,
		0xFF96:0x30E8,
		0xFF97:0x30E9,
		0xFF98:0x30EA,
		0xFF99:0x30EB,
		0xFF9A:0x30EC,
		0xFF9B:0x30ED,
		0xFF9C:0x30EF,
		0xFF9D:0x30F3,
		0xFF9E:0x309B,
		0xFF9F:0x309C,
		0xFF66:0x30F2
};

你可能感兴趣的:(Java,#,JS,java,后端)