出于某种原因,每当我试图在字节字符串的末尾找到斜杠(在本例中,base_url 设置为“www.google.com/”)时,它似乎不想返回它存在或不存在。但是,如果我将 base_url 更改为类似“www.google.com”的内容,其中 m 是最后一个字符,那么它会发现它很好。
//in this case, the base_url is "www.google.com/". I also tried to simply put "www.google.com/" in place of the base_url, just in case.
char * last_char_ptr;
last_char_ptr = strrchr(base_url, '/');
if(last_char_ptr == NULL)
{
cout << "it is null!" << endl;
}
else
{
cout << *last_char_ptr << endl;
}
怎么了?
最佳答案
我不知道你在做什么,但这对我有用:
#include <string.h>
#include <iostream>
int main()
{
char const* ptr = strrchr("google/", '/');
std::cout << (ptr? ptr: "<null>") << "\n";
}
也就是说,它打印一个正斜杠。如果您的代码出现 it is null!
在到达此代码之前出了点问题。
关于c++ - 如何使用 C++ 中的 strrchr 库函数搜索正斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888248/