Created
April 3, 2024 00:11
-
-
Save asdf913/75282a77682aab91d24bab0063e96da2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Objects; | |
import org.apache.commons.lang3.ObjectUtils; | |
import com.github.jikyo.suji.Converter; | |
import com.github.jikyo.suji.Kansuji; | |
public class NumberToKanji { | |
public static void main(final String[] args) { | |
// | |
System.out.println(convertNumberToKanji(null)); | |
// | |
System.out.println(convertNumberToKanji("")); | |
// | |
// 年 | |
// | |
System.out.println(convertNumberToKanji("年")); | |
// | |
// 昭和六十三年 | |
// | |
System.out.println(convertNumberToKanji("昭和63年")); | |
// | |
// 平成三十年 | |
// | |
System.out.println(convertNumberToKanji("平成30年")); | |
// | |
// 令和元年 | |
// | |
System.out.println(convertNumberToKanji("令和元年")); | |
// | |
} | |
private static String convertNumberToKanji(final String string) { | |
// | |
if (string == null) { | |
// | |
return null; | |
// | |
} // if | |
// | |
final List<Kansuji> kansujis = Converter.kansujis(string, false); | |
// | |
Kansuji kansuji = null; | |
// | |
StringBuilder sb = null; | |
// | |
for (int i = 0; kansujis != null && i < kansujis.size(); i++) { | |
// | |
if ((kansuji = kansujis.get(i)) == null | |
|| (sb = ObjectUtils.getIfNull(sb, () -> new StringBuilder(string))) == null) { | |
// | |
continue; | |
// | |
} // if | |
// | |
sb.replace(kansuji.begin(), kansuji.end(), kansuji.value()); | |
// | |
} // for | |
// | |
return Objects.toString(ObjectUtils.getIfNull(sb, () -> new StringBuilder(string))); | |
// | |
} | |
} |
Purpose / 目的
Convert the number within a string into kanji
文字列内の数字を漢字に変換します
Output Sample
null 年 昭和六十三年 平成三十年 令和元年
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required dependencies
https://github.com/jikyo/suji4j