从字符串序列中提取最后 2 个单词,以空格分隔
我有任何序列(或句子),我想提取最后 2 个字符串。
例如,
sdfsdfds sdfs dfsd fgsd 3 dsfds
应生成:3 dsfds
sdfsd (dfgdg)gfdg fg 6 gg
应生成:6 gg
I have any sequence (or sentence) and i want to extract the last 2 strings.
For example,
sdfsdfds sdfs dfsd fgsd 3 dsfds
should produce:3 dsfds
sdfsd (dfgdg)gfdg fg 6 gg
should produce:6 gg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
std::string::find_last_of
查找空格的功能。You can use
std::string::find_last_of
function to find spaces.如果您的字符串以空格分隔,则以下内容将起作用。
The following will work if your strings are whitespace separated.
以错误的顺序返回字符串,但如果这并不重要,
或者,
Returns the strings in the wrong order, but if that doesn't matter,
Alternatively,
我鼓励您查看 Boost 库。它的算法和数据结构可以为您提供极大的帮助。以下是如何使用 Boost.StringAlgo 解决您的问题:
我还鼓励您始终使用 vector::at 方法而不是 []。这将为您提供正确的错误处理。
I would encourage you to have a look at the Boost library. It has algorithms and data structures that help you tremendously. Here's how to solve your problem using Boost.StringAlgo:
I would also encourage you to always use the vector::at method instead of []. This will give you proper error handling.
更简单:)
Simpler :)