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

发布于 2024-09-30 08:07:00 字数 126 浏览 1 评论 0原文

给定如下字符串:

   sdfsd34 
    sdfdsf1

我想提取:使用c++(STL但没有boost)的34, 1,c。

谢谢

Given strings like the following:

   sdfsd34 
    sdfdsf1

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

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

玩套路吗 2024-10-07 08:07:00

您正在搜索函数 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);

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);
苏璃陌 2024-10-07 08:07:00

这里很大程度上取决于您是否更喜欢字符串操作,或者更喜欢 STL 风格的迭代器等。正如 @Konrad 已经指出的那样,std::string 内置了 find_last_not_of,适合任何喜欢字符串样式操作的人。

如果您愿意,您可以使用更像容器的字符串,并从末尾开始查找第一个非数字:

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

在这种情况下,从 reverse_iteratoriterator< /code> 碰巧效果很好。 find_if 返回的 reverse_iterator 将引用字符串末尾的一组连续数字之前的最后一个元素。然而,它的基数将引用第一个数字(或者如果末尾没有数字或者(不幸的是)如果整个字符串由数字组成)。

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).

↘紸啶 2024-10-07 08:07:00

初始版本,使用

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;

编辑:更好地使用成员函数:

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;

只是为了好的措施 - 不需要检查所有数字的 版本。

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;

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;
泪冰清 2024-10-07 08:07:00

这是一个C解决方案。您必须包含 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); 
}

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); 
}
伏妖词 2024-10-07 08:07:00

如果它们总是在末尾,那么只需从字符串的末尾开始查找数字,当您找到第一个非数字时,您就找到了它。看看reverse_find,或者find_reverse,或者类似的东西。我已经很长时间没有在 STL 中进行任何字符串操作了。作为一名老式的 C 程序员,我可能最终会将其转换为 char 数组(如果它不是超长字符串),并执行愚蠢的指针技巧,但这只是我。

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.

依 靠 2024-10-07 08:07:00

假设 str 是有问题的字符串,并且 i 将包含一个指向数字开头的字符的指针......

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

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++;
请爱~陌生人 2024-10-07 08:07:00

已经发布的 C++ 解决方案非常合理。在C语言中,可以使用strpbrk。这给出了任何一组字符的第一次出现,因此如果不能保证数字出现在字符串的末尾,则需要调用 strpbrk 直到它返回 null,从而在下一个调用之前保存上一个调用的结果称呼。没有像 strchr 那样的反向 strpbrk,至少据我所知。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文