12

Given strings like the following:

   sdfsd34 
    sdfdsf1

I would like to extract: 34, 1 using c++ (STL but no boost), c.

thanks

7 Answers 7

35

You’re searching for the function string.find_last_not_of:

string str = "sdfsd34";
size_t last_index = str.find_last_not_of("0123456789");
string result = str.substr(last_index + 1);
2
  • str.substr(str.find_first_of("0123456789")) is OK too.
    – uol3c
    May 3, 2017 at 3:39
  • 2
    @Jeelee that depends on what OP meant. If he meant "extract everything after the first number", yours is right; if he meant "extract trailing numbers" (ie. 2 in 1337hunter2), only Konrad is right. Visitors from the future of course can use whatever they want to do.
    – Joeytje50
    Sep 24, 2018 at 16:27
4

A lot here depends on whether you're more comfortable with string manipulation, or with STL-style iterators and such. As @Konrad already pointed out, std::string has find_last_not_of built in, for anybody who likes string-style manipulation.

If you prefer, you can work with a string a bit more like a container, and find the first non-digit starting from the end:

std::string::iterator pos = std::find_if(
    mystring.rbegin(), 
    mystring.rend(), 
    std::not1(std::isdigit)).base();

In this case, the conversion from reverse_iterator to iterator happens to work out very nicely. The reverse_iterator returned by find_if will refer to the last element before a contiguous set of digits at the end of the string. The base of that, however, will refer to the first of the digits (or mystring.end() if there are no digits at the end or (unfortunately) if the whole string consists of digits).

3

Initial version, using <algorithm>:

string input("aasdf43");
string matches("01234567890");

string::iterator iter = find_first_of(input.begin(), input.end(), 
        matches.begin(), matches.end());

string next(iter, input.end());

stringstream intStream(next);
int intValue;
intStream >> intValue;

EDIT: Better to use member function:

string input("aasdf43");
string matches("0123456789");
size_t offset = input.find_first_of(matches);
string next(input.substr(offset));

stringstream intStream(next);
int intValue;
intStream >> intValue;

Just for good measure - an <algorithm> version not requiring check versus all digits.

string::reverse_iterator iter = find_if_not(input.rbegin(), input.rend(), 
    ([&](char c) { return c >= '0' && c <= '9';}));
reverse(input.rbegin(), iter);
string reversed(input.rbegin(), iter);

stringstream intStream(reversed);
int intValue;
intStream >> intValue;
4
  • Good alternative. Note, however, that this will fail for strings such as "ab12cd34" (i.e. any string that has numbers followed by letters). Nov 1, 2010 at 19:37
  • @Konrad - yes, this is for problem as stated and yours is more robust. Mine is better with member function not <algorithm> version. Nov 1, 2010 at 19:39
  • 1) I liked the algorithms version because it was a nice alternative. It also used iterators which I prefer over indexes any time. 2) Hm. I forgot about that. But yes, it will work since string::npos == -1 as per the standard (§21.3.6). Nov 1, 2010 at 19:43
  • The version from Konrad also works for entirely numeric input, because string::find_last_not_of returns string::npos (== (string::size_type)-1) for not found. When adding 1, you conveniently end up with 0. Nov 1, 2010 at 19:46
2

Here is a C solution. You've to include stdio.h and ctype.h to get this to work.

char* str = "djafldjsalj124"; 
long n; 

char *s = str; 
while (*s && !isdigit(*s)) s++; 
if (*s)
{
 sscanf(s, "%d", &n);  
 printf("%d\n", n); 
}
1

If they're always at the end, then just start at the end of the string and look for a digit, when you get to the first non digit number, then you have it. Look at reverse_find, or find_reverse, or something like that. It's been a long time since I've done any string manipulation in STL. Being the old-timey c programmer that I am, I'd probably end up converting it to a char array (if it's not a super long string), and doing stupid pointer tricks, but thats just me.

1

Assuming str is the string in question, and i will contain a pointer to the character where the number starts...

char* i;
for(i = str + strlen(str) - 1; i >= str && *i >= '0' && *i <= '9'; i--);
i++;
0

The C++ solutions already posted are pretty reasonable. In C, you can use strpbrk. This gives the first occurance of any of a set of characters, so if it not guaranteed that the digits appear at the end of the string, you will need to call strpbrk until it returns null, saving the result of the previous call before the next call. There is no reverse strpbrk as there is for strchr, at least that I know of.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.