c++中想要对字符串string进行截取字符特定的字符字串,可以结合substr()和一系列的返回索引函数来做。
1、substr()函数很好理解,他的原型为:
//原型1
basic strin::substr(string,start,length);
//原型2(将string移到外面)
string &a,
a.substr(start,length);
函数功能时返回字符串a从start位置开始的长为length的子串,其中start参数必须提供,参数lmeiyouength为可选,如果没有提供length参数的话,将返回字符串a从start位置开始一直到字符串a的最后的子串。
2、读于索引查找函数,有多个:
//find()
size_t find (const string& str, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,查找有目标字符串str(string型字符串)的位置。
size_t find (const char* s, size_t pos = 0) const;
//从源字符串起始位置pos(默认为0)处,查找有目标字符串s(char型字符串)的位置。
size_t find (const char* s, size_t pos, size_type n) const;
//从源字符串起始位置pos处,查找有目标字符串s前n个字符(char型字符串)的位置。
size_t find (char c, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,查找有目标字符c的位置。
//find_first_of()
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符在目标字符串str(string型字符
//串)中,则返回首次匹配的该字符的位置。
size_t find_first_of (const char* s, size_t pos = 0) const;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符在目标字符串s(char型字符串)
//中,则返回首次匹配的该字符的位置。
size_t find_first_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符在目标字符串s前n个字符(char型字符串)
//中,则返回首次匹配的该字符的位置。
size_t find_first_of (char c, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符等于目标字符c,则返回首次匹
//配的该字符的位置。
//find_last_of()
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串str(string型字
//符串)中,则返回首次匹配的该字符的位置。
size_t find_last_of (const char* s, size_t pos = npos) const;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串s(char型字符串)
//中,则返回首次匹配的该字符的位置。
size_t find_last_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符在目标字符串s前n个字符(char型字符串)
//中,则返回首次匹配的该字符的位置。
size_t find_last_of (char c, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符等于目标字符c,则返回首次匹
//配的该字符的位置。
//find_first_not_of()
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不在目标字符串str(string型字符
//串)中,则返回首次不匹配的该字符的位置。
size_t find_first_not_of (const char* s, size_t pos = 0) const;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不在目标字符串s(char型字符串)
//中,则返回首次不匹配的该字符的位置。
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符不在目标字符串s前n个字符(char型字符串)
//中,则返回首次不匹配的该字符的位置。
size_t find_first_not_of (char c, size_t pos = 0) const noexcept;
//从源字符串起始位置pos(默认为0)处,依此查找每个字符。如果某字符不等于目标字符c,则返回首次
//不匹配的该字符的位置。
//find_last_not_of()
size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串str(string型
//字符串)中,则返回首次不匹配的该字符的位置。
size_t find_last_not_of (const char* s, size_t pos = npos) const;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串s(char型字符
//串)中,则返回首次不匹配的该字符的位置。
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,依此查找每个字符。如果某字符不在目标字符串s前n个字符(char型字符串)
//中,则返回首次不匹配的该字符的位置。
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不等于目标字符c,则返回首次
//不匹配的该字符的位置。
//rfind()
size_t rfind (const string& str, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,查找有目标字符串str(string型字符串)的位置。
size_t rfind (const char* s, size_t pos = npos) const;
//从源字符串起始位置pos(默认为npos)处,查找有目标字符串s(char型字符串)的位置。
size_t rfind (const char* s, size_t pos, size_t n) const;
//从源字符串起始位置pos处,查找有目标字符串s前n个字符(char型字符串)的位置。
size_t rfind (char c, size_t pos = npos) const noexcept;
//从源字符串起始位置pos(默认为npos)处,查找有目标字符c的位置。
根据自己想要截取什么要的子串,取找合适的find函数,结合substr()函数就能得到自己想要的子串。