SWI Prolog escape quotes

梦想的初衷 提交于 2019-12-14 01:32:52

问题


I need to put " " around a String in prolog. I get the input from another program and as it looks I can't escape the " in this program, so i have to add the " in prolog otherwise the prolog statement doesn't work.

Thanks for your help!


回答1:


For a discussion of strings see here, they are SWI-Prolog specific but use the same escape rules as atoms. There are many ways to enter quotes into an atom in a Prolog text:

1) Doubling them. So for example 'can''t be' is an atom, with a single quote as the fourth character, and no other single quotes in it.

2) Escaping them, with the backslash. So for example 'can\'t be' is the same atom as 'can''t be'.

3) Character coding them, using octal code and a closing back slash. So for example 'can\47\t be' is the same atom as 'can''t be'.

4) Character coding them, using hex code and a closing back slash. So for example 'can\x27\t be' is the same atom as 'can''t be'.

The above possibilities are all defined in the ISO standard. A Prolog implementation might define further possibilities.

Bye

P.S.: Here is an example run in SWI-Prolog, using a different example character. In the first example query below, you don't need doubling, doubling can only be done for the surrounding quote.

The last example query below shows a SWI-Prolog specific syntax which is not found in the ISO standard, namely using a backslash u with a fixed width hex code:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.33)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam

?- X = 'she said "bye"'.
X = 'she said "bye"'.

?- X = 'she said \"bye\"'.
X = 'she said "bye"'.

?- X = 'she said \42\bye\42\'.
X = 'she said "bye"'.

?- X = 'she said \x22\bye\x22\'.
X = 'she said "bye"'.

?- X = 'she said \u0022bye\u0022'.
X = 'she said "bye"'.


来源:https://stackoverflow.com/questions/26504738/swi-prolog-escape-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!