/// <summary> /// 将拼音格式化成指定的格式 /// </summary> /// <param name="py">待格式化的拼音</param> /// <param name="format">格式</param> /// <see cref="ToneFormat"/> /// <see cref="CaseFormat"/> /// <see cref="VCharFormat"/> /// <returns></returns> public static string Format(string py, PinyinFormat format) { // "v"或"u:"不能添加声调 if (format.Contains(WITH_TONE_MARK) && (format.Contains(WITH_V) || format.Contains(WITH_U_AND_COLON))) { throw new PinyinException("\"v\"或\"u:\"不能添加声调"); } var pinyin = py; if (format.Contains(WITHOUT_TONE)) { // 不带声调 var reg = new Regex("[1-5]"); pinyin = reg.Replace(pinyin, ""); } else if (format.Contains(WITH_TONE_MARK)) { // 带声调标志 pinyin = pinyin.Replace("u:", "v"); pinyin = convertToneNumber2ToneMark(pinyin); } if (format.Contains(WITH_V)) { // 输出v pinyin = pinyin.Replace("u:", "v"); } else if (format.Contains(WITH_U_UNICODE)) { // 输出ü pinyin = pinyin.Replace("u:", "ü"); } if (format.Contains(UPPERCASE)) { // 大写 pinyin = pinyin.ToUpper(); } else if (format.Contains(LOWERCASE)) { // 小写 pinyin = pinyin.ToLower(); } else if (format.Contains(CAPITALIZE_FIRST_LETTER)) { // 首字母大写 // 不处理单拼音 a e o if (!IGNORE_LIST.Contains(pinyin.ToLower())) { pinyin = pinyin.Substring(0, 1).ToUpper() + (pinyin.Length == 1 ? "" : pinyin.Substring(1)); } } return(pinyin); }
/// <summary> /// 扩展大小写格式 /// </summary> /// <param name="format"></param> /// <param name="caseSpread"></param> /// <param name="firstLetterOnly"></param> /// <param name="pinyin"></param> /// <returns></returns> public static string SpreadCase(PinyinFormat format, bool caseSpread, bool firstLetterOnly, StringBuilder pinyin) { if (firstLetterOnly || !caseSpread) { return(pinyin.ToString().Trim()); } if (format.Contains(CAPITALIZE_FIRST_LETTER)) { return(CapitalizeFirstLetter(pinyin));; } if (format.Contains(LOWERCASE)) { return(pinyin.ToString().Trim().ToLower()); } if (format.Contains(UPPERCASE)) { return(pinyin.ToString().Trim().ToUpper()); } return(pinyin.ToString().Trim()); }