Check if one string is a prefix of another

前端 未结 13 866
-上瘾入骨i
-上瘾入骨i 2020-12-08 04:21

I have two strings which I\'d like to compare: String and String:. Is there a library function that would return true when passed these two strings

相关标签:
13条回答
  • 2020-12-08 04:47

    Easiest way is to use substr() and compare() member functions:

    string str = "Foobar";
    string prefix = "Foo";
    
    if(str.substr(0, prefix.size()).compare(prefix) == 0) cout<<"Found!";
    
    0 讨论(0)
提交回复
热议问题