如何 trim 在序言中从 String 右空格?
How to trim right whitespaces from String in prolog?
例如,我想 trim 右转字符串中的空格。
"Dog " 变为 "Dog" ,“ ”变为“”。
这是如何实现的?
我知道 SWI prolog 有 trim 空白谓词,但是它 trims String from right and left。
不过我也在使用 GNU prolog,所以希望编写自己的解决方案。
您可以将字符串转换为字符列表(请参阅 convert string to list in prolog),编写谓词以根据需要对其进行操作,然后将结果列表转换回字符串。
以下是在 SWI-Prolog 中使用 DCG 完成此操作的方法。我知道您说过您正在使用 GNU Prolog,但也许这至少对其他使用 SWI-Prolog 的人有帮助:
:- use_module(library(dcg/basics)).
trimmed(S) --> blanks, string(S), blanks, eos, !.
trim(S, T) :-
string_codes(S, C), phrase(trimmed(D), C), string_codes(T, D).
GNU Prolog 是受支持的 Logtalk backend compilers. With it, you gain access to (portable) libraries (and developer tools!) that allows you to easily solve the problem. For example, if your strings are represented using atoms, you can use the atom::replace_sub_atom/4
谓词之一:
| ?- {types(loader)}.
...
yes
| ?- atom::replace_sub_atom(' ', '', 'Dog ', Output).
Output = 'Dog'
yes
| ?- atom::replace_sub_atom(' ', '', ' becomes ', Output).
Output = becomes
如果您使用双引号,它们的含义取决于 double_quotes
标志值,在这种情况下,您可以使用 list::subtract/3
谓词。例如:
| ?- current_prolog_flag(double_quotes, Value).
Value = codes
yes
yes
| ?- list::subtract("Dog ", " ", Output).
Output = [68,111,103]
yes
| ?- list::subtract(" becomes ", " ", Output).
Output = [98,101,99,111,109,101,115]
yes
| ?- set_prolog_flag(double_quotes, chars).
yes
| ?- list::subtract("Dog ", " ", Output).
Output = ['D',o,g]
yes
| ?- list::subtract(" becomes ", " ", Output).
Output = [b,e,c,o,m,e,s]
yes
| ?- set_prolog_flag(double_quotes, atom).
yes
| ?- atom::replace_sub_atom(" ", "", "Dog ", Output).
Output = 'Dog'
yes
| ?- atom::replace_sub_atom(" ", "", " becomes ", Output).
Output = becomes
yes
例如,我想 trim 右转字符串中的空格。 "Dog " 变为 "Dog" ,“ ”变为“”。 这是如何实现的?
我知道 SWI prolog 有 trim 空白谓词,但是它 trims String from right and left。
不过我也在使用 GNU prolog,所以希望编写自己的解决方案。
您可以将字符串转换为字符列表(请参阅 convert string to list in prolog),编写谓词以根据需要对其进行操作,然后将结果列表转换回字符串。
以下是在 SWI-Prolog 中使用 DCG 完成此操作的方法。我知道您说过您正在使用 GNU Prolog,但也许这至少对其他使用 SWI-Prolog 的人有帮助:
:- use_module(library(dcg/basics)).
trimmed(S) --> blanks, string(S), blanks, eos, !.
trim(S, T) :-
string_codes(S, C), phrase(trimmed(D), C), string_codes(T, D).
GNU Prolog 是受支持的 Logtalk backend compilers. With it, you gain access to (portable) libraries (and developer tools!) that allows you to easily solve the problem. For example, if your strings are represented using atoms, you can use the atom::replace_sub_atom/4
谓词之一:
| ?- {types(loader)}.
...
yes
| ?- atom::replace_sub_atom(' ', '', 'Dog ', Output).
Output = 'Dog'
yes
| ?- atom::replace_sub_atom(' ', '', ' becomes ', Output).
Output = becomes
如果您使用双引号,它们的含义取决于 double_quotes
标志值,在这种情况下,您可以使用 list::subtract/3
谓词。例如:
| ?- current_prolog_flag(double_quotes, Value).
Value = codes
yes
yes
| ?- list::subtract("Dog ", " ", Output).
Output = [68,111,103]
yes
| ?- list::subtract(" becomes ", " ", Output).
Output = [98,101,99,111,109,101,115]
yes
| ?- set_prolog_flag(double_quotes, chars).
yes
| ?- list::subtract("Dog ", " ", Output).
Output = ['D',o,g]
yes
| ?- list::subtract(" becomes ", " ", Output).
Output = [b,e,c,o,m,e,s]
yes
| ?- set_prolog_flag(double_quotes, atom).
yes
| ?- atom::replace_sub_atom(" ", "", "Dog ", Output).
Output = 'Dog'
yes
| ?- atom::replace_sub_atom(" ", "", " becomes ", Output).
Output = becomes
yes