Remove/replace C++ 字符串中的多字符 (ÿû)

Remove/replace Multicharacters (ÿû) in a C++ String

我正在尝试使用 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 替换它们的更好选择?

In file included from 4:0:
/usr/include/c++/4.9/tr1/regex:2407:5: warning: inline function '_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) [with _Out_iter = std::back_insert_iterator >; _Bi_iter = __gnu_cxx::__normal_iterator >; _Rx_traits = std::tr1::regex_traits; _Ch_type = char; std::tr1::regex_constants::match_flag_type = std::bitset]' used but never defined
     regex_replace(_Out_iter __out, _Bi_iter __first, _Bi_iter __last,
     ^
/tmp/ccGJXgKd.o: In function `f1()':
:(.text+0x81): undefined reference to `std::tr1::basic_regex >::_M_compile()'
:(.text+0xc5): undefined reference to `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: error: ld returned 1 exit status

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

示例:

#include<iostream>
#include<string>

int main()
{
    std::string str = "ÿûABC";
    std::string remove = "ÿû";

    std::cout << "Length of source string is " << str.length() << " characters\n";
    std::cout << "Length of string to remove is " << remove.length() << " characters\n";

    size_t pos = str.find(remove);
    if (pos == std::string::npos)
    {
        std::cout << "Substring \"ÿû\" not found\n";
    }
    else
    {
        std::cout << "Found sub-string \"" << remove << "\" at position " << pos << '\n';
        str.erase(pos, remove.length());
        std::cout << "After erasing: \"" << str << "\"\n";
    }
}

来自working example的输出:

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 调用获取它们的实际长度。