删除/替换C++字符串中的多字符 (ÿû)



我正在尝试使用 std::tr1::regex 替换字符串中的多字符,因为我找不到任何可以帮助替换它们的函数。代码如下:

// Example program
#include <iostream>
#include <string>
#include <tr1/regex>
void f1()
{
std::string str = "ÿûABC";
std::tr1::regex rx("ÿû");
std::string replacement = "";
std::tr1::regex_replace(str,rx,replacement);
}
int main()
{
f1();
return 0;
}

但是我收到以下编译错误。任何人都可以建议是否有任何解决方法或使用 C++98 替换它们的更好选择?

在 4:0 包含的文件中:/usr/include/c++/4.9/tr1/regex:2407:5: 警告: 内联函数 '_Out_iter std::tr1::regex_replace(_Out_iter, _Bi_iter, _Bi_iter, const std::tr1::basic_regex&, const std::basic_string&, std::tr1::regex_constants::match_flag_type( [_Out_iter = std::back_insert_iterator>; _Bi_iter = __gnu_cxx::__normal_iterator>; _Rx_traits = std::tr1::regex_traits; _Ch_type = 字符; std::tr1::regex_constants::match_flag_type = std::bitset]' 已使用但从未定义  regex_replace(_Out_iter __out、_Bi_iter __first、_Bi_iter __last、  ^/tmp/ccGJXgKd.o: 在函数 'f1((' 中: :(.text+0x81(:未定义对"std::tr1::basic_regex>::_M_compile(("的引用 :(.text+0xc5(:未定义的引用 'std::back_insert_iterator std::tr1::regex_replace, __gnu_cxx::__normal_iterator, std::tr1::regex_traits, char>(std::back_insert_iterator, __gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, std::tr1::basic_regex> const&, std::basic_string, std:::allocator> const&, std::bitset(' collect2:错误:ld 返回 1 个退出状态

要从另一个字符串中删除子字符串,您应该使用erase函数。

例:

#include<iostream>
#include<string>
int main()
{
std::string str = "ÿûABC";
std::string remove = "ÿû";
std::cout << "Length of source string is " << str.length() << " charactersn";
std::cout << "Length of string to remove is " << remove.length() << " charactersn";
size_t pos = str.find(remove);
if (pos == std::string::npos)
{
std::cout << "Substring "ÿû" not foundn";
}
else
{
std::cout << "Found sub-string "" << remove << "" at position " << pos << 'n';
str.erase(pos, remove.length());
std::cout << "After erasing: "" << str << ""n";
}
}

工作示例的输出:

Length of source string is 7 characters
Length of string to remove is 4 characters
Found sub-string "ÿû" at position 0
After erasing: "ABC"

这里要注意的重要部分是字符'ÿ''û'不是单个字节!您的编辑器可能将它们保存为每个两个字节,用 UTF-8 编码。

通过将要删除的子字符串放在其自己的std::string对象中,我们可以轻松地获得erase调用的实际长度。

最新更新