getShortPinyin static method

String getShortPinyin(
  1. String str
)

获取字符串对应拼音的首字母 @param str 需要转换的字符串 @return 对应拼音的首字母 (成都 cd)

Implementation

static String getShortPinyin(String str) {
  if (str.isEmpty) return '';
  StringBuffer sb = StringBuffer();
  StringBuffer temp = StringBuffer();
  for (int i = 0, len = str.length; i < len; i++) {
    String c = str[i];
    if (ChineseHelper.isChinese(c)) {
      int j = i + 1;
      temp.write(c);
      while (j < len && (ChineseHelper.isChinese(str[j]))) {
        temp.write(str[j]);
        j++;
      }
      String pinyin = getPinyin(temp.toString(), separator: pinyinSeparator);
      List<String> pinyinArray = pinyin.split(pinyinSeparator);
      pinyinArray.forEach((v) {
        sb.write(v[0]);
        i++;
      });
      i--;
      temp.clear();
    } else {
      sb.write(c);
    }
  }
  return sb.toString();
}