在字符串中使用\作为文字而不是转义

mon*_*Moo 6 c++ xcode string-literals

bool stringMatch(const char *expr, const char *str) {   
    // do something to compare *(expr+i) == '\\'  
    // In this case it is comparing against a backslash
    // i is some integer
}

int main() {
    string a = "a\sb";
    string b = "a b";
    cout << stringMatch(a.c_str(), b.c_str()) << endl;
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

所以现在的问题是:Xcode没有在'\'中读取,当我在stringMatch函数中调试时,expr只出现'asb'而不是文字a\sb'.

并且Xcode在该行吐出警告:string a ="a\sb":未知的转义序列

编辑:我已经尝试使用"a \\ sb",它以"a \\ sb"作为文字读入.

Der*_*erp 13

bool stringMatch(const char *expr, const char *str) {   
   // do something to compare *(expr+i) == '\\'  
   // In this case it is comparing against a backslash
   // i is some integer
}

int main() {
    string a = "a\\sb";
    string b = "a b";
    cout << stringMatch(a.c_str(), b.c_str()) << endl;
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

默认情况下,C和C++将反斜杠作为转义序列处理.你必须通过在字符串中添加额外的反斜杠来告诉C不要使用反斜杠作为转义序列.

这些是常见的转义序列:

  • \ a - 贝尔(哔)
  • \ b - 退格
  • \ f - Formfeed
  • \n - 新行
  • \ r - 回车
  • \ t - 水平标签
  • \\ - 反斜杠
  • \' - 单引号
  • \" - 双重标记
  • \ ooo - 八进制表示
  • \ xdd - 十六进制表示

编辑: Xcode在您的计算机上表现异常.所以我可以建议你这个.

bool stringMatch(const char *expr, const char *str) {   
   // do something to compare *(expr+i) == '\\'  
   // In this case it is comparing against a backslash
   // i is some integer
}

int main() {
    string a = "a" "\x5C" "sb";
    string b = "a b";
    cout << stringMatch(a.c_str(), b.c_str()) << endl;
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

不要担心string a声明中的空格,Xcode连接用空格分隔的字符串.

编辑2:确实Xcode正在读取你的"a\\b"字面意思,这就是它如何处理转义反斜杠.当你输出string a = "a\\sb"到控制台时,你会看到,a\sb.但是当你string a在方法之间作为参数或私有成员传递时,它将需要额外的反斜杠.您必须考虑到这一事实来设计代码,以便忽略额外的反斜杠.这取决于你如何处理字符串.

编辑3:这 Edit 1是你的最佳答案,但这是另一个.

stringMatch()方法中添加代码以使用单反斜杠替换双反斜杠.

你只需要在函数的最开头添加这个额外的行:

expr=[expr stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
Run Code Online (Sandbox Code Playgroud)

这应该解决双反斜杠问题.

编辑4: 有些人认为编辑3是ObjectiveC,因此不是最优的,所以ObjectiveC++中的另一个选项.

void searchAndReplace(std::string& value, std::string const& search,std::string const& replace)
{
    std::string::size_type  next;

    for(next = value.find(search);        // Try and find the first match
        next != std::string::npos;        // next is npos if nothing was found
        next = value.find(search,next)    // search for the next match starting after
                                          // the last match that was found.
       )
    {
        // Inside the loop. So we found a match.
        value.replace(next,search.length(),replace);   // Do the replacement.
        next += replace.length();                      // Move to just after the replace
                                                       // This is the point were we start
                                                       // the next search from. 
    }
}
Run Code Online (Sandbox Code Playgroud)


编辑5:如果你将const char *in 更改stringMatch()为'string`,它对你来说就不那么复杂了.

expr.replace(/*size_t*/ pos1, /*size_t*/ n1, /*const string&*/ str );
Run Code Online (Sandbox Code Playgroud)

PS你现在有了所有的答案,所以你实施哪一个能给你最好的结果.


bam*_*s53 7

Xcode正在吐出该警告,因为它将\s"a\sb" 解释为转义序列,但\s不是有效的转义序列.它被替换s为字符串变为"asb".

逃避反斜杠"a\\sb"是正确的解决方案.如果这对您不起作用,请发布更多详细信息.

这是一个例子.

#include <iostream>
#include <string>

int main() {
    std::string a = "a\\sb";
    std::cout << a.size() << ' ' << a << '\n';
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出如下:

4 a\sb

如果你得到不同的输出请发布.另外,请准确发布您之前尝试过"a \\ sb"时遇到的问题.


正则表达式可能是C++的痛苦,因为必须以这种方式转义反斜杠.C++ 11有原始字符串,不允许任何类型的转义,因此不需要转义反斜杠:R"(a\sb)".