更换UNI code转义序列字符串 [英] Replace unicode escape sequences in a string

查看:1080
本文介绍了更换UNI code转义序列字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们拥有具有以下文本一个文本文件

We have one text file which has a following text

"\u5b89\u5fbd\u5b5f\u5143"

当我们在阅读C#中的文件CON污点.NET这表明像

When we read a file con taint in c# .net it shows like

"\\u5b89\\u5fbd\\u5b5f\\u5143"

我们的德codeR方法是

Our Decoder Method is

public string Decoder(string value)
        {
            Encoding enc = new UTF8Encoding();
            byte[] bytes = enc.GetBytes(value);
           return enc.GetString(bytes);
        }

当我经过艰苦code值

When I pass hard Code value

string Output=Decoder("\u5b89\u5fbd\u5b5f\u5143");

它工作得很好,但是当我们使用变量值那个时候它不能正常工作。

it works well but when we use variable value that time it is not working.

当我们使用字符串我们从文本文件中获取

When we use the string what we get from text file

  value=(text file containt)
  string Output=Decoder(value);

这将返回错误的输出。

It returns wrong Output.

请帮我解决这个问题。

推荐答案

您可以定期EX pression来解析文件:

You could regular expression to parse the file:

private static Regex _regex = new Regex(@"\\u(?<Value>[a-zA-Z0-9]{4})", RegexOptions.Compiled);
public string Decoder(string value)
{
    return _regex.Replace(
        value,
        m => ((char)int.Parse(m.Groups["Value"].Value, NumberStyles.HexNumber)).ToString()
    );
}

然后:

string data = Decoder(File.ReadAllText("test.txt"));

这篇关于更换UNI code转义序列字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆