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’。
Xcode在行中发出警告: string a= "a\sb“:未知转义序列
编辑:我已经尝试使用"a\sb",它读入为"a\sb“作为文字。
发布于 2012-08-24 05:19:21
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不要使用反斜杠作为转义序列,方法是在字符串中添加一个额外的反斜杠。
以下是常见的转义序列:
>H110\r-回车
编辑: 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:确实是从字面上读取你的"a\\b"
,这就是它处理转义反斜杠的方式。当您将string a = "a\\sb"
输出到控制台时,您将看到a\sb
。但是当你将string a
作为参数或私有成员在方法之间传递时,它将直接使用额外的反斜杠。你必须考虑到这个事实来设计你的代码,这样它就会忽略额外的反斜杠。如何处理字符串由您决定。
EDIT 3: Edit 1
是您在这里的最佳答案,但这里还有另一个答案。
在stringMatch()
方法中添加代码,将双反斜杠替换为单反斜杠。
您只需在函数的最开始添加以下行:
expr=[expr stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
这应该可以解决双反斜杠问题。
EDIT 4:有些人认为Edit 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.
}
}
5:如果你把stringMatch()
中的字符串改为‘const char *
’,对你来说就不那么复杂了。
expr.replace(/*size_t*/ pos1, /*size_t*/ n1, /*const string&*/ str );
EDIT 6:从C++11开始,就存在像raw string literals
这样的东西。这意味着您不必转义,相反,您可以编写以下代码:
string a = R"raw(a\sb)raw";
请注意,字符串中的raw
可以由您选择的任何分隔符替换。这适用于您希望在实际字符串中使用像)raw
这样的子字符串的情况。当您必须大量转义字符时,使用这些原始字符串字面量主要是有意义的,比如结合使用std::regex
。
附言:你现在已经有了所有的答案,所以哪一个能给你带来最好的结果取决于你自己。
发布于 2012-08-24 05:14:07
Xcode之所以发出这个警告,是因为它将"a\sb“中的\s
解释为转义序列,但\s
不是有效的转义序列。它被替换为s
,因此字符串变成了"asb“。
像"a\\sb"
这样转义反斜杠是正确的解决方案。如果这个方法对你不起作用,请发布更多的细节。
下面是一个例子。
#include <iostream>
#include <string>
int main() {
std::string a = "a\\sb";
std::cout << a.size() << ' ' << a << '\n';
}
此程序的输出如下所示:
如果你得到了不同的输出,请发布它。另外,请准确地发布您在之前尝试"a\sb“时观察到的问题。
正则表达式在C++中可能很麻烦,因为反斜杠必须以这种方式进行转义。C++11的原始字符串不允许任何类型的转义,因此转义反斜杠是不必要的:R"(a\sb)"
。
https://stackoverflow.com/questions/12103445
复制