C ++如何在字符后获取子字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28163723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ How to get substring after a character?
提问by SKLAK
For example, if I have
例如,如果我有
string x = "dog:cat";
and I want to extract everything after the ":", and return cat. What would be the way to go about doing this?
我想提取“:”之后的所有内容,然后返回 cat。这样做的方法是什么?
回答by rcs
Try this:
尝试这个:
x.substr(x.find(":") + 1);
回答by Newbie
I know it will be super late but I am not able to comment accepted answer. If you are using only a single character in find
function use ''
instead of ""
.
As Clang-Tidy says The character literal overload is more efficient.
我知道这会很晚,但我无法评论已接受的答案。如果您仅使用单个字符find
函数使用''
代替""
。正如 Clang-Tidy 所说The character literal overload is more efficient.
So
x.substr(x.find(':') + 1)
所以
x.substr(x.find(':') + 1)
回答by Edward Kigwana
The accepted answer from rcscan be improved. Don't have rep so I can't comment on the answer.
来自rcs的公认答案可以改进。没有代表,所以我无法对答案发表评论。
std::string x = "dog:cat";
std::string substr;
auto npos = x.find(":");
if (npos != std::string::npos)
substr = x.substr(npos + 1);
if (!substr.empty())
; // Found substring;
Not performing proper error checking trips up lots of programmers. The string has the sentinel the OP is interested but throws std::out_of_range if pos > size().
不执行正确的错误检查会绊倒很多程序员。该字符串具有 OP 感兴趣的标记,但如果 pos > size() 则抛出 std::out_of_range。
basic_string substr( size_type pos = 0, size_type count = npos ) const;
回答by Trevor Hickey
#include <iostream>
#include <string>
int main(){
std::string x = "dog:cat";
//prints cat
std::cout << x.substr(x.find(":") + 1) << '\n';
}
Here is an implementation wrapped in a function that will work on a delimiter of any length:
这是一个封装在函数中的实现,它可以处理任意长度的分隔符:
#include <iostream>
#include <string>
std::string get_right_of_delim(std::string const& str, std::string const& delim){
return str.substr(str.find(delim) + delim.size());
}
int main(){
//prints cat
std::cout << get_right_of_delim("dog::cat","::") << '\n';
}
回答by Karthik R P
What you can do is get the position of ':' from your string, then retrieve everything after that position using substring.
您可以做的是从字符串中获取 ':' 的位置,然后使用子字符串检索该位置之后的所有内容。
size_t pos = x.find(":"); // position of ":" in str
size_t pos = x.find(":"); // position of ":" in str
string str3 = str.substr (pos);
string str3 = str.substr (pos);
回答by Harikrishnan N
Try this:
尝试这个:
string x="dog:cat";
int pos = x.find(":");
string sub = x.substr (pos+1);
cout << sub;
回答by Brahmanand Choudhary
#include <string>
#include <iostream>
std::string process(std::string const& s)
{
std::string::size_type pos = s.find(':');
if (pos!= std::string::npos)
{
return s.substr(pos+1,s.length());
}
else
{
return s;
}
}
int main()
{
std::string s = process("dog:cat");
std::cout << s;
}
回答by Dyrandz Famador
Try this one..
试试这个..
std::stringstream x("dog:cat");
std::string segment;
std::vector<std::string> seglist;
while(std::getline(x, segment, ':'))
{
seglist.push_back(segment);
}
回答by Cristian Olaru
something like this:
像这样:
string x = "dog:cat";
int i = x.find_first_of(":");
string cat = x.substr(i+1);