简体   繁体   English

如何用转义字符替换字符串中的所有特殊字符?

[英]How do I replace all special characters in a string with the escape character?

If I had a string that looked like \\"A\\\\nB\\" , how do I transform it to look like "A\\nB" ?如果我有一个看起来像\\"A\\\\nB\\"的字符串,我如何将它转换为看起来像"A\\nB"

The \\n portion should work as a new line. \\n部分应该作为一个新行。 It should not be printing "A\\nB", it should be printing as follows:它不应该打印“A\\nB”,它应该打印如下:

A
B

This should do the work:这应该做的工作:

if(example.size() > 1) {
    for (auto i = 0ul; i < example.size() - 1; ++i) // loop through the string char by char
        if (example[i] != '\\' || (example[i + 1] == '\\'))
            result += example[i]; // if the current one is a \ and the next char is different to \ (for the case of \\) remove it
    if (example[example.size() - 1] == '\\' && example[example.size() - 1] == '\\') result += '\\'; // check for the last char
} else if(example.size() == 1 && example[0] != '\\') result+=example[0]; // check for special case there the string is just one char long

You can create a separate function for removing escape characters from your std::string in a following way:您可以通过以下方式创建一个单独的函数来从std::string中删除转义字符:

std::string remove_escape_char(std::string const& s) {
    std::string result;
    auto it = s.begin();
    while (it != s.end()) {
        char c = *it++;
        if (c == '\\' && it != s.end()) {
            switch (*it++) {
            case '\\':
                c = '\\';
                break;
            case 'n':
                c = '\n';
                break;
            default: 
                continue;
            }
        }
        result += c;
    }

    return result;
}

and then the function for removing special characters from std::string :然后是从std::string删除特殊字符的函数:

void remove_special_char(std::string& str, char c) {
    auto position = str.find(c);
    while (position != std::string::npos) {
        str.erase(position, 1);
        position = str.find(c);
    }
}

You can use above two functions like:您可以使用以上两个功能,例如:

std::string str{"\"A\\nB\""};
remove_special_char(str, 0x22); // remove "
std::cout << remove_escape_char(str) << std::endl;

Now the output should be:现在输出应该是:

A
B

Demo 演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转义printf()中的所有特殊字符 - Escape all special characters in printf() 如何用空格替换std :: string中的所有非字母字符(数字和特殊字符) - How to replace all non alpha characters (digits and special characters) from std::string with a space 用特殊字符替换字符串空间 - Replace string spaces with special characters 我正在尝试在C ++中的字符串中查找所有“#”字符,并用“哈希”替换它们,但是正则表达式无法识别字符“#” - I'm trying to find all the “#” characters in a string in C++ and replace them with “hash” but the regex does not recognise the character “#” 如何将带有转义符的C / C ++字符串转换为普通(原始)字符串 - How do I convert C/C++ string with escape character to a plain (raw) string 如何使用std :: string替换所有出现的两个字符的一个字符? - How to replace all occurrences of one character with two characters using std::string? 如何使用 Qt 类和函数构建 XML 文件并转义所有非 Unicode 字符? - How do I build a XML file using Qt classes and functions and escape all non-Unicode characters? 是否有c ++函数用它们的转义序列替换xml特殊字符? - Is there c++ function that replace xml Special Character with their escape sequence? 如何替换字符串中所有出现的字符? - How to replace all occurrences of a character in string? 如何将一个字符串的所有实例替换为另一个字符串? - How do I replace all instances of a string with another string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM