SWI-Prolog:如何从转义字符串中获取 unicode 字符? [英] SWI-Prolog: How to get unicode char from escaped string?
问题描述
我遇到了一个问题,我有一个转义字符串,例如\\u0026",我需要将其转换为 unicode char '\u0026'.
I have a problem, I've got an escaped string for example "\\u0026" and I need this to transform to unicode char '\u0026'.
像这样的技巧string_concat('\\', S, "\\u0026"), write(S).没有帮助,因为它会删除 \ 不仅是 escape .所以基本上我的问题是,如何从字符串中删除转义字符.
Tricks like string_concat('\\', S, "\\u0026"), write(S). didn't help, because it will remove \ not only the escape . So basically my problem is, how to remove escape chars from the string.
哦,我刚刚注意到,stackoverflow 也可以使用转义 \.
Oh, I've just noticed, that stackoverflow also plays with escape \.
write_canonical/1 给了我\\u0026",如何将其转换为单个&"字符?
write_canonical/1 gives me "\\u0026", how to transform that into a single '&' char?
推荐答案
在 ISO Prolog 中,char 通常被认为是长度为 1 的原子.原子和字符用单引号括起来,或者不写如果可能的话,报价.以下是一些示例:
In ISO Prolog a char is usually considered an atom of length 1. Atoms and chars are enclosed in single quotes, or written without quotes if possible. Here are some examples:
?- X = abc. /* an atom, but not a char */
X = abc
?- X = a. /* an atom and also a char */
X = a
?- X = '\u0061'.
X = a
\u 符号是特定于 SWI-Prolog 的,在 ISO 中找不到序言.在 SWI-Prolog 中,再次找不到数据类型字符串在 ISO Prolog 中,并始终用双引号括起来.这里有一些例子:
The \u notation is SWI-Prolog specific, and not found in the ISO Prolog. In SWI-Prolog there is a data type string again not found in the ISO Prolog, and always enclosed in double quotes. Here are some examples:
?- X = "abc". /* a string */
X = "abc"
?- X = "a". /* again a string */
X = "a"
?- X = "\u0061".
X = "a"
如果手头有长度为 1 的字符串,则可以将其转换为字符通过谓词 atom_string/2.这是一个 SWI-Prolog 特定的谓词,不在 ISO Prolog 中:
If you have a string at hand of length 1, you can convert it to a char via the predicate atom_string/2. This is a SWI-Prolog specific predicate, not in ISO Prolog:
?- atom_string(X, "\u0061").
X = a
?- atom_string(X, "\u0026").
X = &
一些建议.首先开始学习 ISO Prolog 原子谓词,有相当多的.然后学习 SWI-Prolog 原子和字符串谓词.
Some recommendation. Start learning the ISO Prolog atom predicates first, there are quite a number. Then learn the SWI-Prolog atom and string predicates.
您不必学习这么多新的 SWI-Prolog 谓词,因为在 SWI-Prolog 中,大多数 ISO Prolog 谓词也接受字符串.下面是在第一个参数中使用字符串的 ISO Prolog 谓词 atom_codes/2 的示例:
You dont have to learn so many new SWI-Prolog predicates, since in SWI-Prolog most of the ISO Prolog predicates also accept strings. Here is an example of the ISO Prolog predicate atom_codes/2 used with a string in the first argument:
?- atom_codes("\u0061\u0026", L).
L = [97, 38].
?- L = [0'\u0061, 0'\u0026].
L = [97, 38].
?- L = [0x61, 0x26].
L = [97, 38].
P.S: ISO Prolog 中定义了 0' 表示法,它既不是字符、原子或字符串,而是代表整数数据类型.该值是 0' 后给定字符的代码.我将它与 SWI-Prolog \u 符号结合起来.
P.S: The 0' notation is defined in the ISO Prolog, its neither a char, atom or string, but it represents an integer data type. The value is the code of the given char after the 0'. I have combined it with the SWI-Prolog \u notation.
P.P.S:\u 符号中的 0' 符号当然是多余的,在 ISO Prolog 中可以直接使用十六进制符号前缀 0x 表示整数值.
P.P.S: The 0' notation in connection of the \u notation is of course redundant, in ISO Prolog one can directly use the hex notation prefix 0x for integer values.
这篇关于SWI-Prolog:如何从转义字符串中获取 unicode 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!