如何从字符串末尾提取数字
给定如下字符串:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在搜索函数
string.find_last_not_of
:You’re searching for the function
string.find_last_not_of
:这里很大程度上取决于您是否更喜欢字符串操作,或者更喜欢 STL 风格的迭代器等。正如 @Konrad 已经指出的那样,
std::string
内置了find_last_not_of
,适合任何喜欢字符串样式操作的人。如果您愿意,您可以使用更像容器的字符串,并从末尾开始查找第一个非数字:
在这种情况下,从
reverse_iterator
到iterator< /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
hasfind_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:
In this case, the conversion from
reverse_iterator
toiterator
happens to work out very nicely. Thereverse_iterator
returned byfind_if
will refer to the last element before a contiguous set of digits at the end of the string. Thebase
of that, however, will refer to the first of the digits (ormystring.end()
if there are no digits at the end or (unfortunately) if the whole string consists of digits).初始版本,使用
:编辑:更好地使用成员函数:
只是为了好的措施 - 不需要检查所有数字的
版本。Initial version, using
<algorithm>
:EDIT: Better to use member function:
Just for good measure - an
<algorithm>
version not requiring check versus all digits.这是一个C解决方案。您必须包含 stdio.h 和 ctype.h 才能使其正常工作。
Here is a C solution. You've to include stdio.h and ctype.h to get this to work.
如果它们总是在末尾,那么只需从字符串的末尾开始查找数字,当您找到第一个非数字时,您就找到了它。看看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.
假设 str 是有问题的字符串,并且 i 将包含一个指向数字开头的字符的指针......
Assuming str is the string in question, and i will contain a pointer to the character where the number starts...
已经发布的 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.