删除Erlang中的字符串的子字符串/字符串模式 [英] Remove a substring/ string pattern of a string in Erlang

查看:210
本文介绍了删除Erlang中的字符串的子字符串/字符串模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml字符串,如

I have an xml string like

 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> ,那么得到一个子列表将会做到:

If you only want to remove the specific string literal </B> then getting a sublist will do the trick:

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 功能:

If you need a more general approach you can use the re:replace/3 function:

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\"/>"

这篇关于删除Erlang中的字符串的子字符串/字符串模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆