Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “如何替换”\&引用;加上\\&引用;在c++;一串_C++_C++11_Visual C++ - Fatal编程技术网

C++ “如何替换”\&引用;加上\\&引用;在c++;一串

C++ “如何替换”\&引用;加上\\&引用;在c++;一串,c++,c++11,visual-c++,C++,C++11,Visual C++,我有字符串我的\u string=“第一个字符串\r\n第二个字符串\r\n第三个字符串”。。。发射型计算机断层扫描仪。 新字符串在\r\n之后开始,我想用\替换\ 我试过:- My\u字符串。替换(“\”,“\”)但它对我不起作用。 还有别的办法吗 如果要将转义字符(\n、\r等)转换为文字反斜杠和[a-z]字符,可以使用switch语句并附加到缓冲区。假设C++标准库字符串,您可以这样做: std::string escaped(const std::string& input) {

我有
字符串我的\u string=“第一个字符串\r\n第二个字符串\r\n第三个字符串”
。。。发射型计算机断层扫描仪。 新字符串在\r\n之后开始,我想用
\
替换
\

我试过:-
My\u字符串。替换(“\”,“\”)但它对我不起作用。

还有别的办法吗

如果要将转义字符(\n、\r等)转换为文字反斜杠和[a-z]字符,可以使用switch语句并附加到缓冲区。假设C++标准库字符串,您可以这样做:

std::string escaped(const std::string& input)
{
    std::string output;
    output.reserve(input.size());
    for (const char c: input) {
        switch (c) {
            case '\a':  output += "\\a";        break;
            case '\b':  output += "\\b";        break;
            case '\f':  output += "\\f";        break;
            case '\n':  output += "\\n";        break;
            case '\r':  output += "\\r";        break;
            case '\t':  output += "\\t";        break;
            case '\v':  output += "\\v";        break;
            default:    output += c;            break;
        }
    }

    return output;
}

这将使用switch语句,并将所有常见转义序列转换为文字“\”和用于表示转义序列的字符。所有其他字符将按原样追加到字符串中。简单、高效、易于使用。

如果要将转义字符(\n、\r等)转换为文字反斜杠和[a-z]字符,可以使用switch语句并附加到缓冲区。假设C++标准库字符串,您可以这样做:

std::string escaped(const std::string& input)
{
    std::string output;
    output.reserve(input.size());
    for (const char c: input) {
        switch (c) {
            case '\a':  output += "\\a";        break;
            case '\b':  output += "\\b";        break;
            case '\f':  output += "\\f";        break;
            case '\n':  output += "\\n";        break;
            case '\r':  output += "\\r";        break;
            case '\t':  output += "\\t";        break;
            case '\v':  output += "\\v";        break;
            default:    output += c;            break;
        }
    }

    return output;
}
这将使用switch语句,并将所有常见转义序列转换为文字“\”和用于表示转义序列的字符。所有其他字符将按原样追加到字符串中。简单、高效、易于使用。

是否尝试此代码

My_string.replace( My_string.begin(), My_string.end(), 'x', 'y'); //replace all occurances of 'x' with 'y'
是否有类似的问题

请尝试此代码

My_string.replace( My_string.begin(), My_string.end(), 'x', 'y'); //replace all occurances of 'x' with 'y'

是一个类似的问题:

是C还是C++?在C.中没有字符串类型,在C或C++中没有任何东西,如<代码> \ \ >代码>。它给你一个转义序列的开始。因此,这里不清楚您要做什么。出现问题的一个原因是字符串中没有反斜杠。如果您有一个字符串文本,那么编译器将用它们的实际编码值替换转义序列(如
“\r\n”
)(使用
的示例“\r\n”
将替换为字符串中的两个字节
13
10
),你没有看到任何可疑的东西吗?投票决定关闭,因为不清楚@Stask:如果你可以用有效的代码更新问题,并且更清楚你想要什么,那么请这样做。是C还是C++?在C.中没有字符串类型,在C或C++中没有任何东西,如<代码> \ \ >代码>。它给你一个转义序列的开始。因此,这里不清楚您要做什么。出现问题的一个原因是字符串中没有反斜杠。如果您有一个字符串文本,那么编译器将用它们的实际编码值替换转义序列(如
“\r\n”
)(使用
的示例“\r\n”
将替换为字符串中的两个字节
13
10
),你没有看到任何可疑的东西吗?投票决定关闭,因为不清楚@堆栈:如果你能用有效代码更新问题,并且更清楚你想要什么,那么请这样做。非常感谢!!!!!它对我有用:)非常感谢你!!!!!它对我有用:)