在字符串中使用as作为文字而不是转义符 [英] using \ in a string as literal instead of an escape

查看:161
本文介绍了在字符串中使用as作为文字而不是转义符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
}

所以现在的问题是:Xcode无法读取' ',当我在stringMatch函数中调试时,expr似乎只是'asb'而不是原义的a\sb'。

So the problem right now is: Xcode is not reading in the '\', when I was debugging in stringMatch function, expr appears only to be 'asb' instead of the literal a\sb'.

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

And Xcode is spitting out an warning at the line: string a = "a\sb" : Unknown escape sequence

编辑:我已经尝试使用 a\\sb

I have already tried using "a\\sb", it reads in as "a\\sb" as literal.

推荐答案

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;
}

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

C and C++ deal with backslashes as escape sequences by default. You got to tell C to not use your backslash as an escape sequence by adding an extra backslash to your string.

以下是常见的转义序列:

These are the common escape sequences:


  • \a-钟声(哔哔声)

  • \b-退格

  • \f-换页

  • \n-换行

  • \r-回车

  • \t-水平制表符

  • \\-反斜杠

  • \'-单引号

  • \-双引号

  • \ooo-八进制表示法

  • \xdd-十六进制表示法

  • \a - Bell(beep)
  • \b - Backspace
  • \f - Formfeed
  • \n - New line
  • \r - Carriage Return
  • \t - Horizontal Tab
  • \\ - Backslash
  • \' - Single Quotation Mark
  • \" - Double Quatation Mark
  • \ooo - Octal Representation
  • \xdd - Hexadecimal Representaion

编辑: Xcode在您的计算机上行为异常,因此,我可以建议您这样做。

Xcode is behaving abnormally on your machine. So I can suggest you this.

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;
}

不用担心字符串中的空格一个声明,Xcode将用空格分隔的字符串连接起来。

Don't worry about the spaces in the string a declaration, Xcode concatenates strings separated with a space.

编辑2:实际上,Xcode正在读取您的 a\\b 从字面上看,这就是处理转义的反斜杠的方式。当您将 string a = a\\sb 输出到控制台时,您会看到 a\sb 。但是,当您在方法之间作为参数或私有成员传递 string a 时,它将在字面上加上多余的反斜杠。您必须考虑到这一事实来设计代码,以便忽略额外的反斜杠。

EDIT 2: Indeed Xcode is reading your "a\\b" literally, that's how it deals with escaped backslashes. When you'll output string a = "a\\sb" to console, you'll see, a\sb. But when you'll pass string a between methods as argument or as a private member then it will take the extra backslash literally. You have to design your code considering this fact so that it ignores the extra backslash. It's upto you how you handle the string.

编辑3: 编辑1

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

Add code in your stringMatch() method to replace double backslashes with single backslash.

您只需要在函数的开头添加以下额外行:

You just need to add this extra line at the very start of the function:

expr=[expr stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];

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

This should solve the double backslash problem.

编辑4:
有些人认为编辑3 是ObjectiveC,因此不是最佳选择,因此是ObjectiveC ++的另一种选择。

EDIT 4: Some people think Edit 3 is ObjectiveC and thus is not optimal, so another option in 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. 
    }
}



编辑5:如果将 stringMatch()中的 const char * 更改为'string`,它将


EDIT 5: If you change the const char * in stringMatch() to 'string` it will be less complex for you.

expr.replace(/*size_t*/ pos1, /*size_t*/ n1, /*const string&*/ str );

编辑6:从C ++ 11开始,存在类似 原始字符串文字
这意味着您不必逃脱,而是可以编写以下内容:

EDIT 6: From C++11 on, there exists something like raw string literals. This means you don't have to escape, instead, you can write the following:

string a = R"raw(a\sb)raw";

请注意,字符串中的 raw 可以由您选择的任何定界符代替。对于这种情况,您想在实际字符串中使用像)raw 这样的子字符串。在必须大量转义字符时(例如与 std :: regex 结合使用),使用这些原始字符串文字通常才有意义。

Note that the raw in the string can be replaced by any delimiter of your choosing. This for the case you want to use a sub string like )raw in the actual string. Using these raw string literals mainly make sense when you have to escape characters a lot, like in combination with std::regex.

PS现在,您已经有了所有答案,因此由您决定实施哪种方案可以带来最佳效果。

这篇关于在字符串中使用as作为文字而不是转义符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆