how to check string start in C++

C++String

C++ Problem Overview


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++ Solutions


Solution 1 - C++

std::string s("Hello world");

if (s.find("Hello") == 0)
{
    std::cout << "String starts with Hello\n";
}

Solution 2 - C++

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).

Solution 3 - C++

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());
}

Solution 4 - C++

The correct solution, as always, comes from Boost: boost::algorithm::starts_with.

Solution 5 - C++

With C++20 you can use std::basic_string::starts_with (or std::basic_string_view::starts_with):

#include <string_view>

std::string_view bigString_v("Winter is gone"); // std::string_view avoids the copy in substr below.
std::string_view smallString_v("Winter");
if (bigString_v.starts_with(smallString_v))
{
    std::cout << "Westeros" << bigString_v.substr(smallString_v.size());
}

Solution 6 - C++

To optimize a little bit:

if ( smallString.size() <= bigString.size() &&
     strncmp( smallString.c_str(), bigString.c_str(), smallString.length() ) == 0 )

Don't forget to #include <cstring> or #include <string.h>

Solution 7 - C++

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

Solution 8 - C++

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.)

Solution 9 - C++

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;
}

Solution 10 - C++

Either create a substring that is the length of your smallString variable, and compare the two. Or do a search for the substring smallString and see if it returns index 0

Solution 11 - C++

http://www.cplusplus.com/reference/string/string/substr/

You can use string.substr() to see any number of characters from any position, or you could use a string.find() member.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsufyan siddiqueView Question on Stackoverflow
Solution 1 - C++Some programmer dudeView Answer on Stackoverflow
Solution 2 - C++Alan StokesView Answer on Stackoverflow
Solution 3 - C++KleistView Answer on Stackoverflow
Solution 4 - C++avakarView Answer on Stackoverflow
Solution 5 - C++Roi DantonView Answer on Stackoverflow
Solution 6 - C++BorzhView Answer on Stackoverflow
Solution 7 - C++rogerlsmithView Answer on Stackoverflow
Solution 8 - C++James KanzeView Answer on Stackoverflow
Solution 9 - C++cute_ptrView Answer on Stackoverflow
Solution 10 - C++Dan FView Answer on Stackoverflow
Solution 11 - C++John HumphreysView Answer on Stackoverflow