将字符串中的html转义字符转换为字符

来源:互联网 发布:r软件干预模型 编辑:程序博客网 时间:2024/04/27 21:19

背景:

解析web的源文件时,发现文本节点的text中有转义字符,比如 Oil & Gas test,其中&对应的字符为&。

那么为了获得正确的text,所以就需要对该text中的转义字符进行转换。

以下代码以转换&为例,仅供参考。


code:

#include <iostream>#include <string>#include <iterator>using namespace std;//将字符串中的&转换为&//可以将此方法扩展到所有的转义字符。。。void main(){string str = "Oil & Gas test";//转义字符&的实际表达是&cout << "str转换之前为:" << str << endl;//string strAfter = ReplaceStr(str);string::iterator it = str.begin();string::iterator end = str.end();string::iterator it_temp = it;string::iterator end_temp = it;for (; it != end; ++it){if ((*it) == '&'){it_temp = it;end_temp = it.operator+=(4);if (*(end_temp) == ';'){end_temp = it.operator+=(1);str.replace(it_temp, end_temp, 1, '&');break;}}}cout << "str转换之后为:" << str << endl;system("pause");}

result:




0 0