0

I have a string and i want to acces the number inside the string.

I have tried something like that,

string s="the number is 2.";
int number= stoi(s[14]);

I just want to access 2. I have used stoi but it hasn't work. what should i do?

cigien
  • 57,834
  • 11
  • 73
  • 112
mucahitrtn
  • 57
  • 1
  • 8

4 Answers4

4

std::stoi expects a std::string, not a char. If you want a single char, you can get it out of the string like this:

int number = std::stoi(s.substr(14,1));

If you have more than 1 digit in the number you want to convert, you can just do:

int number = std::stoi(s.substr(14));

which will extract as many digits after the position as it can, to generate a number.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • This assumes that the number is exactly one character long. If "the number is 10", this will return 1. – Seva Alekseyev May 22 '20 at 15:47
  • True, but OP says they "just want to access 2". – cigien May 22 '20 at 15:48
  • Then an equivalent answer would be "int number=2". :) I'd speculate the OP is after an arbitrary integer, not specifically after 2. – Seva Alekseyev May 22 '20 at 15:50
  • Fair enough, let's see what the OP says. I'll edit the answer if so. – cigien May 22 '20 at 15:52
  • 1
    Constructing a `std::string` just for this is incredibly wasteful. – Asteroids With Wings May 22 '20 at 17:00
  • 1
    @AsteroidsWithWings with small string optimization, likely it's not wasteful at all. And it's not like there is a better alternative short of rolling your own `stoi` which takes a string_view instead of a string. – Aykhan Hagverdili May 22 '20 at 19:38
  • @Ayxan Okay, granted, the single-character version might not be _so_ bad if SSO is in play. The second example not necessarily so, though. Sadly, the need for null termination makes [rolling your own a bit of a faff...](https://stackoverflow.com/q/56634507/4386278) – Asteroids With Wings May 22 '20 at 20:07
  • @AsteroidsWithWings Seems a bit like a premature optimization to me. And anyway, at that point, showing that option would be beyond the scope of this question. – cigien May 22 '20 at 20:10
  • Yeah, it would be. Still, there's a difference between "premature optimization" and "not needlessly writing inefficient code" ;) – Asteroids With Wings May 22 '20 at 20:17
  • Absolutely :) In this case, I think this is about as efficient as it needs to be, unless shown to be a bottleneck. – cigien May 22 '20 at 20:19
  • @ast I don't agree. Any number you can possibly pass is less than 20 digits long. Copying that with SSO is super fast, a single memcpy, and as I said, it's not like there is a convenient and more efficient alternative. – Aykhan Hagverdili May 22 '20 at 20:33
  • @Ayxan The input could be 4GB long. You'd be copying that to extract max 20 chars. It doesn't scale. – Asteroids With Wings May 23 '20 at 13:00
  • @ast I've been thinking about this and my conclusion is that the best solution is to use [Remy Lebeau's answer](https://stackoverflow.com/a/61962368/10147399) and maybe consider [from_chars](https://en.cppreference.com/w/cpp/utility/from_chars). – Aykhan Hagverdili May 23 '20 at 13:02
  • 1
    @Ayxan Yep, I think so too. – Asteroids With Wings May 23 '20 at 13:03
2

You can convert char digits to its numerical value by using a simple arithmetic conversion from char to int:

std::string s ="the number is 2.";
int number = s[14] - '0';
anastaciu
  • 23,467
  • 7
  • 28
  • 53
1

You can use std::string::find_first_of() to find the position of the first digit, then use std::string::find_first_not_of() to find the position after the last digit, and then use std::string::substr() to extract a sub-string containing just the digits, and then finally use std::stoi() to convert the sub-string into an int:

string s = "the number is 2.";
auto start = s.find_first_of("0123456789");
auto end = s.find_first_not_of("0123456789", start);
int number = stoi(s.substr(start, end-start));

Alternatively, after using std::string::find_first_of() to find the position of the first digit, you can then pass std::string::c_str() offset by the position to std::strtol():

string s = "the number is 2.";
auto start = s.find_first_of("0123456789");
int number = std::strtol(str.c_str() + start, nullptr, 10);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

For a conversion logic that doesn't assume a single digit number, here's a slightly more universal snippet:

string s="the number is 234.";
int number= atoi(s.c_str() + 14);
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281