Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在字符串中使用Unicode撇号_Flutter - Fatal编程技术网

Flutter 在字符串中使用Unicode撇号

Flutter 在字符串中使用Unicode撇号,flutter,Flutter,我希望这是一个简单的问题,我只是没有看到森林,因为所有的树木 我有一个颤振字符串,它来自REST API,如下所示: 这是什么 \u正在导致问题 我不能在字符串上执行string.replaceAll\,\操作,因为单斜杠表示它正在查找后面的字符,这不是我需要的 我尝试执行string.replaceAllString.fromCharCode0x92来删除它-但没有成功 然后,我尝试使用正则表达式将其删除,如string.replaceAll/?:\/,但仍保留相同的单斜杠 所以,问题是如何删

我希望这是一个简单的问题,我只是没有看到森林,因为所有的树木

我有一个颤振字符串,它来自REST API,如下所示: 这是什么

\u正在导致问题

我不能在字符串上执行string.replaceAll\,\操作,因为单斜杠表示它正在查找后面的字符,这不是我需要的

我尝试执行string.replaceAllString.fromCharCode0x92来删除它-但没有成功

然后,我尝试使用正则表达式将其删除,如string.replaceAll/?:\/,但仍保留相同的单斜杠

所以,问题是如何删除这个单斜杠,这样我就可以添加一个双斜杠,或者用一个双斜杠替换它

干杯


我发现了这个问题。我在找十六进制92 0x92,它应该是十进制92

我最终解决了这样的问题

String removeUnicodeApostrophes(String strInput) {
    // First remove the single slash.
    String strModified = strInput.replaceAll(String.fromCharCode(92), "");
    // Now, we can replace the rest of the unicode with a proper apostrophe.
    return strModified.replaceAll("u0027", "\'");
}

当读取字符串时,我假设它被解释为文字而不是应该是代码点,即\0027的每个字符都是一个单独的字符。实际上,根据访问API的方式,您可能能够修复此问题-请参见dart。如果对原始数据使用utf8.decode,则可以避免整个问题

然而,如果这不是一个选项,那么有一个足够简单的解决方案

当你写出你的正则表达式或者替换的时候,你并没有逃过反斜杠,所以它实际上变成了一无所有。如果使用双斜杠,则会在转义转义字符时解决问题。\\=>\

另一个选项是使用原始字符串,如r\n,它忽略转义字符

将其粘贴到:

要查看结果,请执行以下操作:

Original encoded properly: What's this?
Replaced with nothing: Whats this?
Using char code for ': Whats this?
Data as read with escaped unicode: What\u0027s this?
Data replaced with apostraphe: What's this?
Data replaced with nothing: Whats this?
Data replaced using raw string: What's this?

谢谢我刚刚尝试了ut8.decode方法,但是json.decode方法失败了。现在,我将坚持我以前发现的,因为它确实正确解码。根据您正在解析的字符串的长度,您可能会在这里产生相当大的开销。通过执行两个单独的replaceAll调用,字符串必须被完全读取两次,并使字符串被复制两次。至少可以使用String.fromCharCode92+u0027,也可以使用“\”正确地转义它,或者使用原始字符串。
Original encoded properly: What's this?
Replaced with nothing: Whats this?
Using char code for ': Whats this?
Data as read with escaped unicode: What\u0027s this?
Data replaced with apostraphe: What's this?
Data replaced with nothing: Whats this?
Data replaced using raw string: What's this?