找到字符串的第一个字符,然后将其与符号c ++进行比较

时间:2017-12-08 02:01:43

标签: c++

尝试检查字符串的第一个字符,看它是否包含" /"。

string pathname = "/test";
if(pathname.at(0) == "/")
{
    // if first character is a slash then delete the slash
    // but only delete the slash if it's the first character
    pathname.erase(pathname.begin()+0);
}

我知道上面的代码不起作用,因为pathname.at(0)被认为是int。

我确定以前曾经问过这个问题。但我看了很多。我已经看到了查找方法和其他许多方法的substr方法。我似乎无法让他们正常工作。

连连呢?提前谢谢。

2 个答案:

答案 0 :(得分:3)

使用单引号作为字符常量。

if(pathname.at(0) == '/')
                     ^ ^

双引号,无论内部有多少个字符,都代表 C风格的字符串。您无法将字符与C字符串进行比较。

答案 1 :(得分:0)

将第一个符号代码与指向string的指针进行比较。尝试将第一个符号与char进行比较。

if(pathname.at(0) == '/')
相关问题