• Chinese converted into pinyin


    /// <summary>
    /// 完整转换函数,输出全拼的结果
    /// </summary>
    /// <param name="CnString">传入的中文字符串</param>
    /// <returns>转换出的拼音组合</returns>
    public static string FullConvert(string CnString)
    {
        byte[] btArray = new byte[2];
        int cAscii = 0;
        short idx1, idx2;
        StringBuilder sbResult = new StringBuilder();
        char[] tempCArray = CnString.ToCharArray();
    
        for (int idx = 0; idx < tempCArray.Length; idx++)
        {
            btArray = Encoding.Default.GetBytes(tempCArray[idx].ToString());
            if (btArray.Length == 1)
                sbResult.Append(tempCArray[idx]);
            else
            {
                idx1 = (short)btArray[0];
                idx2 = (short)btArray[1];
                cAscii = idx1 * 256 + idx2 - 65536;
                if (cAscii > 0  &&  cAscii < 160)
                    sbResult.Append(tempCArray[idx]);
                else
                {
                    for (int i = pyValueArr.Length - 1; i >= 0; i--)
                    {
                        if (pyValueArr[i] <= cAscii)
                        {
                            sbResult.Append(pyCharacterArr[i]);
                            break;
                        }
                    }
                }
            }
        }
        return sbResult.ToString();
    }
    
    /// <summary>
    /// 只输出首字母的组合
    /// </summary>
    /// <param name="CnString">待转换的中文字符串</param>
    /// <returns>拼音首字母组合结果</returns>
    public static string CapitalCovert(string CnString)
    {
        StringBuilder sbTemp = new StringBuilder();
        for (int i = 0; i < CnString.Length; i++)
        {
            sbTemp.Append(GetCnCharAreaCode(CnString.Substring(i, 1)));
        }
    
        return sbTemp.ToString();
    }

    使用也非常简单:


    Console.WriteLine("输出汉字的全拼:");
    Console.WriteLine(DeltaCat.Net.Cn2Py.FullConvert("三角猫"));
    
    Console.WriteLine("输出汉字的首字母组合:");
    Console.WriteLine(DeltaCat.Net.Cn2Py.CapitalCovert("三角猫 @ 真U14.CN"));

    三角猫 真有意思 汉字转拼音




  • 相关阅读:
    nodejs难点
    react 组件化
    vue router & vuex
    vue源码思考
    cookie & session
    servlet
    Hashmap
    Zookeeper+Kafka+flink+socket
    flink consumer can realize kafka avro with overwriting kafkaDeseriler, and executor multithread and genretic extends
    flink kafka consumer with avro schema. handling null
  • 原文地址:https://www.cnblogs.com/Fandyx/p/2241067.html
Copyright © 2020-2023  润新知