Check if one string is a prefix of another

前端 未结 13 865
-上瘾入骨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:25

    How about simply:

    bool prefix(const std::string& a, const std::string& b) {
      if (a.size() > b.size()) {
        return a.substr(0,b.size()) == b;
      }
      else {
        return b.substr(0,a.size()) == a;
      }
    }
    

    C++ not C, safe, simple, efficient.

    Tested with:

    #include <string>
    #include <iostream>
    
    bool prefix(const std::string& a, const std::string& b);
    
    int main() {
      const std::string t1 = "test";
      const std::string t2 = "testing";
      const std::string t3 = "hello";
      const std::string t4 = "hello world";
      std::cout << prefix(t1,t2) << "," << prefix(t2,t1) << std::endl;
      std::cout << prefix(t3,t4) << "," << prefix(t4,t3) << std::endl;
      std::cout << prefix(t1,t4) << "," << prefix(t4,t1) << std::endl;
      std::cout << prefix(t1,t3) << "," << prefix(t3,t1) << std::endl;
    
    }
    
    0 讨论(0)
  • 2020-12-08 04:27

    Use std::mismatch. Pass in the shorter string as the first iterator range and the longer as the second iterator range. The return is a pair of iterators, the first is the iterator in the first range and the second, in the second rage. If the first is end of the first range, then you know the the short string is the prefix of the longer string e.g.

    std::string foo("foo");
    std::string foobar("foobar");
    
    auto res = std::mismatch(foo.begin(), foo.end(), foobar.begin());
    
    if (res.first == foo.end())
    {
      // foo is a prefix of foobar.
    }
    
    0 讨论(0)
  • 2020-12-08 04:29

    You can use this:

    for c++14 or less

    bool has_prefix
        (const std::string& str, const std::string& prefix)  {
        return str.find(prefix, 0) == 0;
    }
    

    for c++17

    //it's a little faster
    auto has_prefix
        (const std::string& str, const std::string_view& prefix) -> decltype(str.find(prefix) == 0) {
        return str.find(prefix, 0) == 0;
    }
    
    0 讨论(0)
  • 2020-12-08 04:30

    If you know which string is shorter, the procedure is simple, just use std::equal with the shorter string first. If you don't, something like the following should work:

    bool
    unorderIsPrefix( std::string const& lhs, std::string const& rhs )
    {
        return std::equal(
            lhs.begin(),
            lhs.begin() + std::min( lhs.size(), rhs.size() ),
            rhs.begin() );
    }
    
    0 讨论(0)
  • 2020-12-08 04:31

    I think strncmp is the closest to what you're looking for.

    Though, if reworded, you may be looking for strstr(s2,s1)==s2, which is not necessarily the most performant way to do that. But you do not want to work out n ;-)

    Okay, okay, the c++ version would be !s1.find(s2).

    Okay, you can make it even more c++, something like this: std::mismatch(s1.begin(),s1.end(),s2.begin()).first==s1.end().

    0 讨论(0)
  • 2020-12-08 04:32

    str1.find(str2) returns 0 if entire str2 is found at the index 0 of str1:

    #include <string>
    #include <iostream>
    
    // does str1 have str2 as prefix?
    bool StartsWith(const std::string& str1, const std::string& str2)
    {   
        return (str1.find(str2)) ? false : true;
    }
    
    // is one of the strings prefix of the another?
    bool IsOnePrefixOfAnother(const std::string& str1, const std::string& str2)
    {   
        return (str1.find(str2) && str2.find(str1)) ? false : true;
    }
    
    int main()
    {
        std::string str1("String");
        std::string str2("String:");
        std::string str3("OtherString");
    
        if(StartsWith(str2, str1))
        {
            std::cout << "str2 starts with str1" << std::endl;      
        }
        else
        {
            std::cout << "str2 does not start with str1" << std::endl;
        }
    
        if(StartsWith(str3, str1))
        {
            std::cout << "str3 starts with str1" << std::endl;      
        }
        else
        {
            std::cout << "str3 does not start with str1" << std::endl;
        }
    
            if(IsOnePrefixOfAnother(str2, str1))
            {
                std::cout << "one is prefix of another" << std::endl;      
            }
            else
            {
                std::cout << "one is not prefix of another" << std::endl;
            }
    
            if(IsOnePrefixOfAnother(str3, str1))
            {
                std::cout << "one is prefix of another" << std::endl;      
            }
            else
            {
                std::cout << "one is not prefix of another" << std::endl;
            }
    
        return 0;
    }
    

    Output:

      str2 starts with str1
      str3 does not start with str1
      one is prefix of another
      one is not prefix of another
    
    0 讨论(0)
提交回复
热议问题