gpt4 book ai didi

functional-programming - 在 Erlang 中删除字符串的子字符串/字符串模式

转载 作者:行者123 更新时间:2023-12-02 01:03:17 24 4
gpt4 key购买 nike

我有一个类似的 xml 字符串

 S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/></B>".

我想删除结束标记 </B>

 S2 = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/>"

我怎样才能做到这一点?

最佳答案

如果您只想删除特定的字符串文字 </B>然后得到一个子列表就可以了:

S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/></B>",
lists:sublist(S, 1, length(S) - 4).
%%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"

如果您需要更通用的方法,可以使用 re:replace/3功能:

S1 = re:replace(S, "</B>", ""),
S2 = iolist_to_binary(S1),
binary_to_list(S2).
%%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"

更新

如评论中所述,提供选项 {return, list}更干净:

re:replace(S, "</B>", "", [{return,list}]).
%%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"

关于functional-programming - 在 Erlang 中删除字符串的子字符串/字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25703670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com