Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of strtol on std::string

Tags:

c++

string

I recently migrated from C to C++, and there's a little confusion about strings. Strings just aren't what they used to be any more, as in, not just char arrays with a terminating '\0'. I haven't found a real answer to this question, so how far can you treat the std::string class like C-Strings?

For example: If I know there's a number somewhere in a string, let the string be ireallylike314, in C I could use strtol(string + 10, NULL, 10) to just get that number. And, if this doesn't work, is there a way to use std::string like C-strings?

like image 542
BigBadWolf Avatar asked Feb 10 '23 16:02

BigBadWolf


1 Answers

Use c_str().

strtol(string.c_str() + 10, NULL, 10);
like image 104
timrau Avatar answered Feb 28 '23 12:02

timrau