Using \ in a string as a literal instead of escape

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

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

And Xcode spits out a warning in the line: string a = "a \ sb": Unknown escape sequence

Edit: I already tried using "a \\ sb", it reads as "a \\ sb" as a literal.

+6
source share
2 answers
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 and C ++ by default have a backslash as escape sequences. You should tell C not to use a backslash as an escape sequence by adding an extra backslash to your string.

Here are the general escape sequences:

  • \ a - ()
  • \ b - Backspace
  • \ 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;
}

string a, XCode , .

2: , Xcode "a\\b", , . string a = "a\\sb" , a\sb. string a , . , . , .

3: Edit 1 , .

stringMatch(), .

:

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

.

4: , Edit 3 - ObjectiveC, , Objective C++.

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: const char * stringMatch() "string", .

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

6: C++ 11, - raw string literals. , , :

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

, raw . , )raw . , , std::regex.

, , , .

+13

Xcode , \s "a\sb" escape-, \s escape-. s, "asb".

, "a\\sb", . - , .

.

#include <iostream>
#include <string>

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

:

4 a \ sb

, . , , , , "a\\sb".


++, . ++ 11 , - , : R"(a\sb)".

+7

All Articles