how to check string start in C++

2020-01-26 06:31发布

Is there any way in C++ to check whether a string starts with a certain string (smaller than the original) ? Just like we can do in Java

bigString.startswith(smallString);

标签: c++ string
12条回答
Melony?
2楼-- · 2020-01-26 06:34

The approaches using string::find() or string::substr() are not optimal since they either make a copy of your string, or search for more than matches at the beginning of the string. It might not be an issue in your case, but if it is you could use the std::equal algorithm. Remember to check that the "haystack" is at least as long as the "needle".

#include <string>    

using namespace std;

bool startsWith(const string& haystack, const string& needle) {
    return needle.length() <= haystack.length() 
        && equal(needle.begin(), needle.end(), haystack.begin());
}
查看更多
啃猪蹄的小仙女
3楼-- · 2020-01-26 06:36

You can do this with string::compare(), which offers various options for comparing all or parts of two strings. This version compares smallString with the appropriate size prefix of bigString (and works correctly if bigString is shorter than smallString):

bigString.compare(0, smallString.length(), smallString) == 0

I tend to wrap this up in a free function called startsWith(), since otherwise it can look a bit mysterious.

UPDATE: C++20 is adding new starts_with and ends_with functions, so you will finally be able to write just bigString.starts_with(smallString).

查看更多
别忘想泡老子
4楼-- · 2020-01-26 06:37

The simplest approach would be:

if ( smallString.size() <= bigString.size()
    && std::equals( smallString.begin(), smallString.end(), bigString.end() )

(This will also work if one of the two, or both, is a vector. Or any other standard container type.)

查看更多
别忘想泡老子
5楼-- · 2020-01-26 06:38
std::string s("Hello world");

if (s.find("Hello") == 0)
{
    std::cout << "String starts with Hello\n";
}
查看更多
Lonely孤独者°
6楼-- · 2020-01-26 06:40

strstr() returns a pointer to the first occurrence of a string within a string.

查看更多
▲ chillily
7楼-- · 2020-01-26 06:40

I thought it makes sense to post a raw solution that doesn't use any library functions...

// Checks whether `str' starts with `start'
bool startsWith(const std::string& str, const std::string& start) {
    if (&start == &str) return true; // str and start are the same string
    if (start.length() > str.length()) return false;
    for (size_t i = 0; i < start.length(); ++i) {
        if (start[i] != str[i]) return false;
    }
    return true;
}

Adding a simple std::tolower we can make this case insensitive

// Checks whether `str' starts with `start' ignoring case
bool startsWithIgnoreCase(const std::string& str, const std::string& start) {
    if (&start == &str) return true; // str and start are the same string
    if (start.length() > str.length()) return false;
    for (size_t i = 0; i < start.length(); ++i) {
        if (std::tolower(start[i]) != std::tolower(str[i])) return false;
    }
    return true;
}
查看更多
登录 后发表回答