如何在Erlang中删除字符串中的引号?

0vvn1miw  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(179)

我正在使用epgsql查询一个Erlang数据库,它返回的字符串结果包括引号,如下所示:"what_was_in_the_row"。如何在处理结果之前删除这些引号?

vyu0f0g1

vyu0f0g11#

the control format ~p (pretty print) keeps information about the type of the variables that are printed. You are printing a string, which is, in erlang, a standard integer list made of printable characters for example [101,102,103] is displayed as "efg" in the shell.
If you call io_lib:format("~p",[[101,102,103]]). , you will get the result ["\"efg\""] which needs some explanation.

  • The surrounding brackets indicate a list, which is the return value type of io_lib:format/2 ,
  • then the first double quotes indicates another list that maybe a string, it is syntactic sugar, but it is not part of the result (I say maybe because any list that looks like a string will be displayed as a string with pretty print),
  • finally the \" is an escape sequence that represents the single character "

You can evaluate the length of each string to verify the difference:

  • 3 = length("efg").
  • 5 = length(""efg"").

If you want to get the string without the surrounding quotes, you must use the control format ~s

1> [Res] = io_lib:format("~s",[[101,102,103]]).
["efg"]
2> length(Res).
3
3> [Res] = io_lib:format("~s",[Res]).          
["efg"]

Of course, the last line shows that my example is stupid. With io_lib:format("~s ~s!",["Hello","world"]). or any more complex formatting, the result is of type chars() : a list of char() or chars() .

相关问题