C++ : string beginWith and endWith

  Peter        2012-08-31 06:53:44       7,975        1    

C++ is an very powerful programming language. It is efficient and flexible. When writing C++ programs, we may often need to process strings and often we need to check whether a string begin with some substring or end with some substring. We can use following functions to ahieve these:

    static bool beginWith(const std::string str,const std::string needle){
        return (!str.compare(0,needle.length(),needle));
    }

    static bool endWith(const std::string str,const std::string needle){
        if (str.length() >= needle.length()) {
            return (0 == str.compare (str.length() - needle.length(), needle.length(), needle));
        }
        return false;
    }

C++  BEGINWITH  ENDWITH 

       

  RELATED


No related articles

  1 COMMENT


Kaley [Reply]@ 2018-05-26 11:45:46
Great article, just what I needed. tottenham tröja


  RANDOM FUN

Santa in Beijing after a busy day

It's a sarcastic picture describing how heavy the haze is in Beijing on Christmas day. Every winter people in Beijing will suffer the heavy haze.