简体   繁体   English

在Dart中替换双反斜杠

[英]Replace double backslash in Dart

I have this escaped string: 我有这个转义的字符串:

\Д\л\я \п\р\о\д\а\ж\и \н\е\д\в\и\ж\и\м\о\с\т\и \\ u0414 \\ u043B \\ u044F \\ u043F \\ u0440 \\ u043E \\ u0434 \\ u0430 \\ u0436 \\ u0438 \\ u043D \\ u0435 \\ u0434 \\ u0432 \\ u0438 \\ u0436 \\ u0438 \\ u043C \\ u043E \\ u043E \\ u0441 \\ u0442 \\ u0438

If I do: 如果我做:

print('\u0414\u043B\u044F \u043F\u0440\u043E\u0434\u0430\u0436\u0438 \u043D\u0435\u0434\u0432\u0438\u0436\u0438\u043C\u043E\u0441\u0442\u0438');

Console will show me: 控制台将向我显示:

Для продажи недвижимости

But if I get escaped 2 times string from the server: 但是,如果我从服务器中逃脱了2次字符串:

\\\Д\\\л\\\я \\\п\\\р\\\о\\\д\\\а\\\ж\\\и \\\н\\\е\\\д\\\в\\\и\\\ж\\\и\\\м\\\о\\\с\\\т\\\и \\\\ u0414 \\\\ u043B \\\\ u044F \\\\ u043F \\\\ u0440 \\\\ u043E \\\\ u0434 \\\\ u0430 \\\\ u0436 \\\\ u0438 \\\\ u043D \\\\ u0435 \\\\ u0434 \\\\ u0432 \\\\ u0438 \\\\ u0436 \\\\ u0438 \\\\ \\\\ u043C \\\\ u043E \\\\ u0441 \\\\ u0442 u0438

And do some replace job: 并做一些替换工作:

var result = string.replaceAll(new RegExp(r'\\'), r'\');

Compiler will not decode those characters and will show same escaped string: 编译器不会解码这些字符,并且会显示相同的转义字符串:

print(result);

Console: 安慰:

\Д\л\я \п\р\о\д\а\ж\и \н\е\д\в\и\ж\и\м\о\с\т\и \\ u0414 \\ u043B \\ u044F \\ u043F \\ u0440 \\ u043E \\ u0434 \\ u0430 \\ u0436 \\ u0438 \\ u043D \\ u0435 \\ u0434 \\ u0432 \\ u0438 \\ u0436 \\ u0438 \\ u043C \\ u043E \\ u043E \\ u0441 \\ u0442 \\ u0438

How I can remove those redunant slashes? 我如何删除那些多余的斜杠?

In string literals in Dart source files, is a literal representing a unicode code point, whereas in the case of data returned from the server, you're just getting back a string containing backslashes, u s, and digits that looks like a bunch of unicode code point literals. 在Dart源文件的字符串文字中, 是表示Unicode代码点的文字,而对于从服务器返回的数据,您只是获取了一个字符串,其中包含反斜杠, u和数字, 看起来像一堆unicode代码点文字。

The ideal fix is to have your server return the UTF-8 string you'd like to display rather than a string that uses Dart's string literal syntax that you need to parse. 理想的解决方法是让您的服务器返回要显示的UTF-8字符串,而不是使用需要解析的Dart字符串文字语法的字符串。 Writing a proper parser for such strings is fairly involved. 为此类字符串编写适当的解析器是相当麻烦的。 You can take a look at unescapeCodeUnits in the Dart SDK for an example. 您可以查看Dart SDK中的unescapeCodeUnits作为示例。

A very inefficient (not to mention entirely hacky and unsafe for real-world use) means of decoding this particular string would be to extract the string representations of the unicode codepoints with a RegExp parse the hex to an int, then use String.fromCharCode() . 解码此特定字符串的一种非常低效的方法(更不用说在实际应用中完全不安全且不安全),将是使用RegExp将十六进制解析为int来提取unicode码点的字符串表示形式,然后使用String.fromCharCode()

Note: the following code is absolutely not safe for production use and doesn't match other valid Dart code point literals such as \\u{1f601} , or reject entirely invalid literals such as \￿ffffff . 注意:以下代码绝对不能用于生产环境 ,并且与其他有效的Dart代码点文字(例如\\u{1f601}不匹配,或者拒绝完全无效的文字(例如\￿ffffff

// Match \u0123 substrings (note this will match invalid codepoints such as \u123456789).
final RegExp r = RegExp(r'\\\\u([0-9a-fA-F]+)');

// Sample string to parse.
final String source = r'\\u0414\\u043B\\u044F \\u043F\\u0440\\u043E\\u0434\\u0430\\u0436\\u0438 \\u043D\\u0435\\u0434\\u0432\\u0438\\u0436\\u0438\\u043C\\u043E\\u0441\\u0442\\u0438';

// Replace each \u0123 with the decoded codepoint.
final String decoded = source.replaceAllMapped(r, (Match m) {
  // Extract the parenthesised hex string. '\\u0123' -> '123'.
  final String hexString = m.group(1);

  // Parse the hex string to an int.
  final int codepoint = int.parse(hexString, radix: 16);

  // Convert codepoint to string.
  return String.fromCharCode(codepoint);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM