static void Main(string[] args) { string A = "\\u4e59\\u70ef"; Console.WriteLine(A); string B = A.Replace(@"\\", @"\"); Console.WriteLine(B); string R =string.Empty; string[] splits = A.Split(new string[] { "\\u" }, StringSplitOptions.RemoveEmptyEntries); foreach (string var in splits) { if (var != "\\u") { R += char.ConvertFromUtf32(int.Parse(var, System.Globalization.NumberStyles.HexNumber)); } } Console.WriteLine(R); Console.ReadLine(); }
十分感谢大家的帮助:) 尤其是LALAC的方法,简单,明了.十分感谢
using System; using System.Collections.Generic; using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string A = "\\u4e59\\u70ef"; string b = A.Replace(@"\\",@"\"); Console.WriteLine(b); Console.ReadLine(); } } }
string A = "\\u4e59\\u70ef"; ----------------------- 这里字符串其实就是\u4e59\u70ef Console.WriteLine(A); //输出:\u4e59\u70ef string B = A.Replace(@"\\", @"\"); ------------------------------------ 这一句其实什么都没替换,因为A里根本没有\\ Console.WriteLine(B); //输出:\u4e59\u70ef 你要把\u4e59\u70ef输出成“乙烯”,恐怕只靠转义是不行的,你看看Converter、Encoding下有什么方法,我觉得出路应该在这里,或者你把\u4e59\u70ef先转换成相应的byte数组,再进行其他转换。
byte[] b = new byte[] {0x59,0x4e,0xef,0x70 }; string s =Encoding.Unicode.GetString(b); Console.WriteLine(s); 这样就可以输出“乙烯”了 。你要做的就是把"\\u4e59\\u70ef";柴分道byte数组里,这你应该知道怎么做了吧
页面输出不就是\u4e59\u70ef吗
\是转意字符
也会得到\u4e59\u70ef
如何利用A,使用一个方法,得到结果:
"\u4e59\u70ef".
(C#, VS2005)首先感谢大家的回复.其次谈谈的我看法:Console.WriteLine(A); 结果是:\\u4e59\\u70ef
string B = "\u4e59\u70ef";
Console.WriteLine(B); 结果是:乙烯
大家可以自己测试一下.A.Replace(@"\\",@"\") 这个方法是不行的.我想如果经过VS的测试,就知道结果是不行的.
还有Remove(),Replace(),SubString(),利用.NET提供的方法是不行的.小弟我招都想尽了,头都大了,要不怎么折磨我一天呢.最后,提出我的方法,经过VS测试,是OK的:
string _t7 = "\\u4e59\\u70ef";
string _t8 = "\u4e59\u70ef";
string _t9 = ""; _t9 = _t7.Replace("\\u", ""); Console.WriteLine(_t9.Length);
string _t10 = CodingToCharacter(_t9); Console.WriteLine(_t7);
Console.WriteLine(_t8);
Console.WriteLine(_t9);
Console.WriteLine(_t10);
Console.ReadLine();
以上代码是MAIN()函数里的.而CodingToCharacter()方法则定义如下:
public static string CodingToCharacter(string coding)
{
string characters = "";
if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。
{
throw new System.Exception("编码格式不正确");
}
for (int i = 0; i < coding.Length; i += 4) //每四位为一个汉字
{
byte[] bytes = new byte[2];
string highCode = coding.Substring(i, 2); //取出低字节,并以16进制进制转换
string lowCode = coding.Substring(i + 2, 2); //取出高字节,并以16进制进行转换
bytes[1] = System.Convert.ToByte(highCode, 16);
bytes[0] = System.Convert.ToByte(lowCode, 16);
string character = System.Text.Encoding.Unicode.GetString(bytes);
characters += character;
}
return characters;
}
OK,成功.大家可以粘贴完了测试一下,谢谢
我的方法测试是OK的,得到结果如下:
8
\u4e59\u70ef
乙烯
4e5970ef
乙烯我要的就是乙烯这个结果,而想得到这个结果,字符串则是"\u4e59\u70ef
",而我从数据源里读数据,只能读到"\\u4e59\\u70ef"
static void Main(string[] args)
{
string A = "\\u4e59\\u70ef";
Console.WriteLine(A); string B = A.Replace(@"\\", @"\");
Console.WriteLine(B); string R =string.Empty;
string[] splits = A.Split(new string[] { "\\u" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string var in splits)
{
if (var != "\\u")
{
R += char.ConvertFromUtf32(int.Parse(var, System.Globalization.NumberStyles.HexNumber));
}
}
Console.WriteLine(R);
Console.ReadLine();
}
尤其是LALAC的方法,简单,明了.十分感谢
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string A = "\\u4e59\\u70ef";
string b = A.Replace(@"\\",@"\");
Console.WriteLine(b);
Console.ReadLine();
}
}
}
其次要说明几点,我不知道大家的Replace(@"\\",@"\")方法用起来效果是如何的.
但是在我这里,在我的机器window xp(sp2) + VS 2005 + EastAsia LanguagePack:
string A = "\\u4e59\\u70ef";
Console.WriteLine(A); //输出:\u4e59\u70ef string B = A.Replace(@"\\", @"\");
Console.WriteLine(B); //输出:\u4e59\u70ef
而用Remove(),SubString()等等,都是不可行的.
我老板说是采用国际通用标准Unicode编码,格式为\uxxx,但是我得到的总是\\uxxx.数据源为EXCEL的内的脚本.在EXCEL里的显示为:rows=[
#代码 名称 组 输出组内明细
('BC=','乙烯', 0, 0),
('NA1','石脑油', 0, 0),
('LHC','轻烃', 0, 0),
('AGO','AGO', 0, 0).......]
当然,我只拿出"乙烯"这两个汉字进行举例操作.
最后十分感谢大家关注.
不结帖的原因是因为我觉得可能还会有更好的答案.谢谢大家
-----------------------
这里字符串其实就是\u4e59\u70ef
Console.WriteLine(A); //输出:\u4e59\u70ef
string B = A.Replace(@"\\", @"\");
------------------------------------
这一句其实什么都没替换,因为A里根本没有\\
Console.WriteLine(B); //输出:\u4e59\u70ef 你要把\u4e59\u70ef输出成“乙烯”,恐怕只靠转义是不行的,你看看Converter、Encoding下有什么方法,我觉得出路应该在这里,或者你把\u4e59\u70ef先转换成相应的byte数组,再进行其他转换。
string s =Encoding.Unicode.GetString(b);
Console.WriteLine(s);
这样就可以输出“乙烯”了 。你要做的就是把"\\u4e59\\u70ef";柴分道byte数组里,这你应该知道怎么做了吧