查找字符串的第一个字符,然后将其与符号c ++进行比较 [英] Find first character of string, then compare it with a symbol c++
本文介绍了查找字符串的第一个字符,然后将其与符号c ++进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试检查字符串的第一个字符以查看其是否包含"/".
Attempting to check the first character of a string to see if it contains a "/".
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)被认为是一个整数.
I understand that the above code will not work because pathname.at(0) is considered an int.
我确定这已经被问过了.但是我环顾了很多.我已经看到了substr方法,find方法和许多其他方法.我似乎无法使他们正常工作.
I'm sure this has been asked before. But I have looked around a lot. I've seen the substr method the find method and many others. I can't seem to get them working right though.
建议?预先感谢.
推荐答案
对字符常量使用单引号.
if(pathname.at(0) == '/')
^ ^
无论内部有多少个字符,双引号都表示一个 C风格的字符串.您无法将字符与C字符串进行比较.
Double quotes, no matter how many characters are inside, represents a C-style string. You can't compare a character to a C-string.
这篇关于查找字符串的第一个字符,然后将其与符号c ++进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文