简体   繁体   English

如何从字符串末尾提取数字

[英]How to extract digit(s) from the end of string

Given strings like the following: 给定如下字符串:

   sdfsd34 
    sdfdsf1

I would like to extract: 34, 1 using c++ (STL but no boost), c. 我想提取: 34,使用c ++ 1 (STL但没有提升),c。

thanks 谢谢

You're searching for the function string.find_last_not_of : 您正在搜索函数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);

A lot here depends on whether you're more comfortable with string manipulation, or with STL-style iterators and such. 这在很大程度上取决于您是对字符串操作还是对STL样式的迭代器等更熟悉。 As @Konrad already pointed out, std::string has find_last_not_of built in, for anybody who likes string-style manipulation. 正如@Konrad已经指出的那样, std::string内置了find_last_not_of ,适合喜欢字符串样式操作的任何人。

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. 在这种情况下,从reverse_iteratoriterator的转换恰好很好地完成了。 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. find_if返回的reverse_iterator将引用字符串末尾一组连续数字之前的最后一个元素。 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). 但是,其base将引用第一个数字(如果末尾没有数字,则引用mystring.end()如果整个字符串由数字组成,则不幸的是)。

Initial version, using <algorithm> : 初始版本,使用<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. 只是出于很好的考虑- <algorithm>版本不需要对所有数字进行校验。

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;

Here is a C solution. 这是一个C解决方案。 You've to include stdio.h and ctype.h to get this to work. 您必须包括stdio.h和ctype.h才能使其正常工作。

char* str = "djafldjsalj124"; 
long n; 

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

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. 看一下reverse_find或find_reverse或类似的东西。 It's been a long time since I've done any string manipulation in STL. 自从我在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. 作为我的老式C程序员,我可能最终会将其转换为char数组(如果它不是一个超长字符串),然后做一些愚蠢的指针技巧,但这就是我。

The C++ solutions already posted are pretty reasonable. 已经发布的C ++解决方案非常合理。 In C, you can use strpbrk. 在C语言中,您可以使用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. 这会给出任何一组字符中的第一个出现,因此,如果不能保证数字出现在字符串的末尾,则需要调用strpbrk直到返回空值,然后保存下一个调用之前的结果呼叫。 There is no reverse strpbrk as there is for strchr, at least that I know of. 至少没有我知道的那样,没有strchr的反向strpbrk。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM