Quantcast
Channel: How do I check if a C++ std::string starts with a certain string, and convert a substring to an int? - Stack Overflow
Browsing latest articles
Browse All 26 View Live

Answer by Toby Speight for How do I check if a C++ std::string starts with a...

From C++23 onwards, you can use std::ranges::starts_with(), which works on any sequence type, including std::string and std::string_view, in any combination:#include <algorithm>#include...

View Article



Answer by TarmoPikaro for How do I check if a C++ std::string starts with a...

C++20, use s.starts_with(), see link for examples:https://en.cppreference.com/w/cpp/string/basic_string/starts_with

View Article

Answer by Guss for How do I check if a C++ std::string starts with a certain...

C++20 update :Use std::string::starts_withhttps://en.cppreference.com/w/cpp/string/basic_string/starts_withstd::string str_value = /* smthg */;const auto starts_with_foo =...

View Article

Answer by CM777 for How do I check if a C++ std::string starts with a certain...

In C++20 now there is starts_with available as a member function of std::string defined as:constexpr bool starts_with(string_view sv) const noexcept;constexpr bool starts_with(CharT c) const...

View Article

Answer by Sam Kumar for How do I check if a C++ std::string starts with a...

Starting with C++20, you can use the starts_with method.std::string s = "abcd";if (s.starts_with("abc")) { ...}

View Article


Answer by Melroy van den Berg for How do I check if a C++ std::string starts...

With C++11 or higher you can use find() and find_first_of()Example using find to find a single char:#include <string>std::string name = "Aaah";size_t found_index = name.find('a');if (found_index...

View Article

Answer by Parker for How do I check if a C++ std::string starts with a...

In case you need C++11 compatibility and cannot use boost, here is a boost-compatible drop-in with an example of usage:#include <iostream>#include <string>static bool starts_with(const...

View Article

Answer by alextoind for How do I check if a C++ std::string starts with a...

Since C++11 std::regex_search can also be used to provide even more complex expressions matching. The following example handles also floating numbers thorugh std::stof and a subsequent cast to...

View Article


Answer by Roi Danton for How do I check if a C++ std::string starts with a...

With C++17 you can use std::basic_string_view& with C++20 std::basic_string::starts_with or std::basic_string_view::starts_with. The benefit of std::string_view in comparison to std::string -...

View Article


Answer by Macsinus for How do I check if a C++ std::string starts with a...

text.substr(0, start.length()) == start

View Article

Answer by Shital Shah for How do I check if a C++ std::string starts with a...

I use std::string::compare wrapped in utility method like below:static bool startsWith(const string& s, const string& prefix) { return s.size() >= prefix.size() && s.compare(0,...

View Article

Answer by mois for How do I check if a C++ std::string starts with a certain...

std::string text = "--foo=98";std::string start = "--foo=";if (text.find(start) == 0){ int n = stoi(text.substr(start.length())); std::cout << n << std::endl;}

View Article

Answer by Ludovic Aubert for How do I check if a C++ std::string starts with...

Use rfind overload that takes the search position pos parameter, and pass zero for it:std::string s = "tititoto";if (s.rfind("titi", 0) == 0) { // pos=0 limits the search to the prefix // s starts with...

View Article


Answer by Marcelo Cantos for How do I check if a C++ std::string starts with...

Given that both strings —argv[1] and "--foo"— are C strings, @FelixDombek's answer is hands-down the best solution.Seeing the other answers, however, I thought it worth noting that, if your text is...

View Article

Answer by Carl Cook for How do I check if a C++ std::string starts with a...

Why not use gnu getopts? Here's a basic example (without safety checks):#include <getopt.h>#include <stdio.h>int main(int argc, char** argv){ option long_options[] = { {"foo",...

View Article


Answer by matiu for How do I check if a C++ std::string starts with a certain...

Nobody used the STL algorithm/mismatch function yet. If this returns true, prefix is a prefix of 'toCheck':std::mismatch(prefix.begin(), prefix.end(), toCheck.begin()).first == prefix.end()Full example...

View Article

Answer by szx for How do I check if a C++ std::string starts with a certain...

You can also use strstr:if (strstr(str, substr) == substr) { // 'str' starts with 'substr'}but I think it's good only for short strings because it has to loop through the whole string when the string...

View Article


Answer by Huseyin Yagli for How do I check if a C++ std::string starts with a...

Code I use myself:std::string prefix = "-param=";std::string argument = argv[1];if(argument.substr(0, prefix.size()) == prefix) { std::string argumentValue = argument.substr(prefix.size());}

View Article

Answer by razvanco13 for How do I check if a C++ std::string starts with a...

Using STL this could look like:std::string prefix = "--foo=";std::string arg = argv[1];if (prefix.size()<=arg.size() && std::equal(prefix.begin(), prefix.end(), arg.begin())) {...

View Article

Answer by Felix Dombek for How do I check if a C++ std::string starts with a...

Just for completeness, I will mention the C way to do it:If str is your original string, substr is the substring you want to check, then strncmp(str, substr, strlen(substr))will return 0 if str starts...

View Article

Answer by Nils for How do I check if a C++ std::string starts with a certain...

Ok why the complicated use of libraries and stuff? C++ String objects overload the [] operator, so you can just compare chars.. Like what I just did, because I want to list all files in a directory and...

View Article


Answer by Tom for How do I check if a C++ std::string starts with a certain...

At the risk of being flamed for using C constructs, I do think this sscanf example is more elegant than most Boost solutions. And you don't have to worry about linkage if you're running anywhere that...

View Article


Answer by blwy10 for How do I check if a C++ std::string starts with a...

if(boost::starts_with(string_to_search, string_to_look_for)) intval = boost::lexical_cast<int>(string_to_search.substr(string_to_look_for.length()));This is completely untested. The principle is...

View Article

Answer by Thomas for How do I check if a C++ std::string starts with a...

You would do it like this:std::string prefix("--foo=");if (!arg.compare(0, prefix.size(), prefix)) foo_value = std::stoi(arg.substr(prefix.size()));Looking for a lib such as Boost.ProgramOptions that...

View Article

Answer by Ferruccio for How do I check if a C++ std::string starts with a...

If you're already using Boost, you can do it with boost string algorithms+boost lexical cast:#include <boost/algorithm/string/predicate.hpp>#include <boost/lexical_cast.hpp>try { if...

View Article


How do I check if a C++ std::string starts with a certain string, and convert...

How do I implement the following (Python pseudocode) in C++?if argv[1].startswith('--foo='): foo_value = int(argv[1][len('--foo='):])(For example, if argv[1] is --foo=98, then foo_value is 98.)Update:...

View Article
Browsing latest articles
Browse All 26 View Live


Latest Images