c++ - 如何使用 C++ 中的 strrchr 库函数搜索正斜杠?

标签 c++ syntax

出于某种原因,每当我试图在字节字符串的末尾找到斜杠(在本例中,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/

相关文章:

java - 为什么遵循控制结构条件的声明需要在 block 中?

javascript - 如何理解kineticjs的语法?

javascript - 当我简单地更改两个表达式的顺序时,为什么这个 for 循环不起作用

c++ - 将 v8 对象转换为自定义 C++ 对象

c++ - 我们如何在 openGL 中相对于一个点(而不是原点)旋转 2d 中的图形?

c++ - 指向多维数组的指针

javascript - ‘::’(双冒号)在 JavaScript 中起什么作用?

c++ - 声明 RValue 方法(例如 void opera() &&;) virtual C++1x 有意义

c++ - 如何使用 XCB 将按键事件发送到应用程序?

Azure ApplicationInsights 指标语法? - 内部服务器故障