Check if one string is a prefix of another

前端 未结 13 868
-上瘾入骨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!";
    

提交回复
热议问题