getPinyinE static method
String
getPinyinE(
- String str, {
- String separator = ' ',
- String defPinyin = ' ',
- PinyinFormat format = PinyinFormat.WITHOUT_TONE,
将字符串转换成相应格式的拼音 (不能转换的字拼音默认用' '替代 ) @param str 需要转换的字符串 @param separator 拼音分隔符 def: ' ' @param defPinyin 默认拼音 def: ' ' @param format 拼音格式 def: PinyinFormat.WITHOUT_TONE @return 字符串的拼音(成都 cheng du)
Implementation
static String getPinyinE(
String str, {
String separator = ' ',
String defPinyin = ' ',
PinyinFormat format = PinyinFormat.WITHOUT_TONE,
}) {
if (str.isEmpty) return '';
StringBuffer sb = StringBuffer();
str = ChineseHelper.convertToSimplifiedChinese(str);
int strLen = str.length;
int i = 0;
while (i < strLen) {
String subStr = str.substring(i);
MultiPinyin? node = convertToMultiPinyin(subStr, separator, format);
if (node == null) {
String _char = str[i];
if (ChineseHelper.isChinese(_char)) {
List<String> pinyinArray = convertToPinyinArray(_char, format);
if (pinyinArray.isNotEmpty) {
sb.write(pinyinArray[0]);
} else {
sb.write(defPinyin);
print(
"### Can't convert to pinyin: $_char , defPinyin: $defPinyin");
}
} else {
sb.write(_char);
}
if (i < strLen) {
sb.write(separator);
}
i++;
} else {
sb.write(node.pinyin);
i += node.word!.length;
}
}
String res = sb.toString();
return ((res.endsWith(separator) && separator != '')
? res.substring(0, res.length - 1)
: res);
}