问题描述:在c#中调用第三方程序,传输的字符串有时会变成unicode字符串如 '\u6d4b\u8bd5\u65f6\u95f4'
问题分析:unicode 编码可以直接转成char 既可,所以可以按照字符串处理,每个字符追加既可。
下面以数组的unicode处理为例:收到数组字符串如下:
List[str]([u'\u6d4b\u8bd5\u65f6\u95f4', u'\u6d4b\u8bd5\u5730\u70b9', u'\u6d4b\u8bd5\u4eba\u5458'])
只需要把字符串中的\u6d4b 做替换既可 程序如下:
private string getDecoderStr(string str)
{
string newStr = "";
//str = @"[16:26:12:156] List[str]([u'\u6d4b\u8bd5\u65f6\u95f4', u'\u6d4b\u8bd5\u5730\u70b9', u'\u6d4b\u8bd5\u4eba\u5458'])";
//var startIndex = str.IndexOf("([");
int strLength = str.Length;
int strIndex = 0;
while (strIndex < strLength)
{
var startIndex1 = str.IndexOf("u'");
if (startIndex1 == -1)
{
newStr += str;
break;
}
newStr += str.Substring(0, startIndex1 + 2);
str = str.Substring(startIndex1 + 2);
strIndex += (startIndex1 + 2);
var endIndex1 = str.IndexOf("'");
string temStr = str.Substring(0, endIndex1);
str = str.Substring(endIndex1+1);
strIndex += endIndex1;
string decodeStr = "";
var itemStrs = temStr.Replace("\\", "").Split('u');
for (int j = 1; j < itemStrs.Length; j++)
{
decodeStr += (char)int.Parse(itemStrs[j], System.Globalization.NumberStyles.HexNumber);
}
newStr += decodeStr;
}
newStr = newStr.Replace("u'","");
return newStr;
}