1.字符串中的英文双引号变成中文双引号

/// <summary>
/// 替换字符串中的英文双引号为中文双引号
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ReplaceYinHaoEnToCn(string str)
{
    string newStr = "";
    string[] array = str.Split('"');
    for (int i = 1; i <= array.Length; i++)
    {
        if (i % 2 == 0)
        {
            newStr += array[i - 1] + "";
        }
        else
        {
            newStr += array[i - 1] + "";
        }
    }
    return newStr.Substring(0, newStr.Length - 1);
}

2.json字符串属性值中的英文双引号变成中文双引号

/// <summary>
/// json字符串将属性值中的英文双引号变成中文双引号
/// </summary>
/// <param name="strJson">json字符串</param>
/// <returns></returns>
public string JsonReplaceSign(string strJson)
{
    //获取每个字符
    char[] temp = strJson.ToCharArray();

    //获取字符数组长度
    int n = temp.Length;

    //循环整个字符数组
    for (int i = 0; i < n; i++)
    {
        //查找json属性值(:+" )
        if (temp[i] == ':' && temp[i+1] == '"')
        {
            //循环属性值内的字符(:+2 推算到value值)
            for (int j = i+2; j < n; j++)
            {
                //判断是否是英文双引号
                if (temp[j] == '"')
                {
                    //排除json属性的双引号
                    if (temp[j+1] != ',' && temp[j+1] != '}')
                    {
                        //替换成中文双引号
                        temp[j] = '';
                    }
                    else if (temp[j+1] == ',' || temp[j+1] == '}')
                    {
                        break;
                    }
                }
                else if (true)
                {
                    // 要过虑其他字符,继续添加判断就可以
                }
            }
        }
    }
    return new String(temp);
}
posted on 2023-05-26 15:46  £冷☆月№  阅读(244)  评论(0编辑  收藏  举报