【50分】一个很让人汗颜的初级问题,同志们帮忙

cnwhitewolf 2008-05-27 01:33:36
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";

如何把str 转换为 "\u5f00\u53d1\u8bed\u8a00";

由于反斜杠是转义字符,汗颜,不知道如何操作

...全文
561 40 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
Guo_881017163 2011-01-12
  • 打赏
  • 举报
回复
开始向各位学习!
mxftzjz45 2008-06-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wwbjj007 的回复:]
引用 2 楼 namhyuk 的回复:
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";

string strNew = str.Replace(@"\\", @"\");
Response.Write(strNew);
[/Quote]


觉得也是
不过还有点看不懂
xzqnwcf 2008-05-28
  • 打赏
  • 举报
回复
同意37楼
wuyi8808 2008-05-27
  • 打赏
  • 举报
回复
// 发现一个更简单的方法:System.Text.RegularExpressions.Regex.Unescape()

class Test
{
static void Main()
{
string str = @"\x41 B C \r\n\u5f00\u53d1\u8bed\u8a00aa";
string str1 = System.Text.RegularExpressions.Regex.Unescape(str);
System.Console.WriteLine("[{0}] -> [{1}]", str, str1);
}
}
/* 程序输出:
[\x41 B C \r\n\u5f00\u53d1\u8bed\u8a00aa] -> [A B C
开发语言aa]
*/
hwk5 2008-05-27
  • 打赏
  • 举报
回复
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";
程序中处理为 \u5f00\u53d1\u8bed\u8a00
LZ想要的是什么结果?
huifeidebaobao 2008-05-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wwbjj007 的回复:]
引用 2 楼 namhyuk 的回复:
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";

string strNew = str.Replace(@"\\", @"\");
Response.Write(strNew);
[/Quote]
就这个
ericzhangbo1982111 2008-05-27
  • 打赏
  • 举报
回复
CompilerResults
这个。。很好很强大 。
cnwhitewolf 2008-05-27
  • 打赏
  • 举报
回复
非常感谢大家,
特别感谢wuyi8808,问题解决!!!!
Jinglecat 2008-05-27
  • 打赏
  • 举报
回复
看不懂,明明就是需要 \\
ProjectDD 2008-05-27
  • 打赏
  • 举报
回复
\\还用转吗?它自己必须这样啊,自己就转了\是转义符号啊等于是说


@"x:\a\b\c" = "x:\\a\\b\\c";
wuyi8808 2008-05-27
  • 打赏
  • 举报
回复
using System; 
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

class Test
{
static void Main()
{
string s = @"\x41 B C \u5f00\u53d1\r\n\""换行\""\x8bed\u8a00aa";
string str1 = Convert(s);
Console.WriteLine("[{0}] -> [{1}]", s, str1);
}

static string Convert(string s)
{
return (new MyString(s)).Parse();
}
}

sealed class MyString
{
object instance;
MethodInfo method;

public MyString(string s)
{
string code = string.Format("sealed class MyString{{public string Parse(){{return \"{0}\";}}}}", s);
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = true;
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, code);
instance = cr.CompiledAssembly.CreateInstance("MyString");
method = instance.GetType().GetMethod("Parse");
}

public string Parse()
{
return (string)method.Invoke(instance, null);
}
}

/* 程序输出:
[\x41 B C \u5f00\u53d1\r\n\"换行\"\x8bed\u8a00aa] -> [A B C 开发
"换行"语言aa]
*/
tianwei262 2008-05-27
  • 打赏
  • 举报
回复
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";
string str1=str.Replace("\\",@"\");
Console.WriteLine(str1);
HarleyTung 2008-05-27
  • 打赏
  • 举报
回复
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";

string strNew = str.Replace(@"\\", @"\");
Response.Write(strNew);
wuyi8808 2008-05-27
  • 打赏
  • 举报
回复
using System; 
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

class Test
{
static void Main()
{
string str = @"\u5f00\u53d1\r\n\""换行\""\x8bed\u8a00";
string str1 = Convert(str);
Console.WriteLine("[{0}] -> [{1}]", str, str1);
}

static string Convert(string s)
{
return (new MyString(s)).Parse();
}
}

sealed class MyString
{
object instance;
MethodInfo method;

public MyString(string expression)
{
string className = "MyString";
string methodName = "Parse";
string code = string.Format("using System;sealed class {0}{{public string {1}(){{return \"{2}\";}}}}",
className, methodName, expression);
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = true;
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(p, code);
if(cr.Errors.Count > 0)
{
string msg = "MyString(\"" + expression + "\"): \n";
foreach (CompilerError err in cr.Errors) msg += err.ToString() + "\n";
throw new Exception(msg);
}
instance = cr.CompiledAssembly.CreateInstance(className);
method = instance.GetType().GetMethod(methodName);
}

public string Parse()
{
return (string)method.Invoke(instance, null);
}
}

/* 程序输出:
[\u5f00\u53d1\r\n\"换行\"\x8bed\u8a00] -> [开发
"换行"语言]
*/
wangfeizjk 2008-05-27
  • 打赏
  • 举报
回复
str.Replace("\\\\","\\"); 用这个试试
lijingziyou 2008-05-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 prewin 的回复:]
都说的麻烦了,其实LZ意思用一个@就满足了
string str = @"\u5f00\u53d1\u8bed\u8a00";
[/Quote]
wuhaiwuya 2008-05-27
  • 打赏
  • 举报
回复
str=@"\a\b\b"
aisiliuzhou 2008-05-27
  • 打赏
  • 举报
回复
我顶啊!
waslee 2008-05-27
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 wuyi8808 的回复:]
我猜LZ是要实现这个目的吧:(至于怎么实现,正在考虑中)

C# codeusing System;

class Test
{
static void Main()
{
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";
string str1 = Convert(str);
Console.WriteLine("[{0}] -> [{1}]", str, str1);
}

static string Convert(string s)
{
return "\u5f00\u53d1\u8bed\u8a00";
}
}
/* 程序输出:
[\u5f00\u53d1\u8bed\u8a0…
[/Quote]
wuyi8808 2008-05-27
  • 打赏
  • 举报
回复
我猜LZ是要实现这个目的吧:(至于怎么实现,正在考虑中)
using System;

class Test
{
static void Main()
{
string str = "\\u5f00\\u53d1\\u8bed\\u8a00";
string str1 = Convert(str);
Console.WriteLine("[{0}] -> [{1}]", str, str1);
}

static string Convert(string s)
{
return "\u5f00\u53d1\u8bed\u8a00";
}
}
/* 程序输出:
[\u5f00\u53d1\u8bed\u8a00] -> [开发语言]
*/
加载更多回复(20)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧