我在c++中使用以下方法解析字符串:
using namespace std;
string parsed,input="text to be parsed";
stringstream input_stringstream(input);
if (getline(input_stringstream,parsed,' '))
{
// do some processing.
}
使用单个字符分隔符进行解析是可以的。但是如果我想使用字符串作为分隔符呢?
例子:我想拆分:
scott>=tiger
用>=作为分隔符,这样我就可以得到斯科特和老虎。
对于字符串(或单个字符)分隔符,这应该非常有效。不要忘记包含#include <sstream>。
std::string input = "Alfa=,+Bravo=,+Charlie=,+Delta";
std::string delimiter = "=,+";
std::istringstream ss(input);
std::string token;
std::string::iterator it;
while(std::getline(ss, token, *(it = delimiter.begin()))) {
std::cout << token << std::endl; // Token is extracted using '='
it++;
// Skip the rest of delimiter if exists ",+"
while(it != delimiter.end() and ss.peek() == *(it)) {
it++; ss.get();
}
}
第一个while循环使用字符串分隔符的第一个字符提取一个标记。第二个while循环跳过分隔符的其余部分,停在下一个标记的开头。
这是一个完整的方法,它在任何分隔符上分割字符串,并返回分割后的字符串的向量。
这是改编自ryanbwork的答案。然而,他的检查:if(token != mystring)给出错误的结果,如果你的字符串中有重复的元素。这是我对那个问题的解决方案。
vector<string> Split(string mystring, string delimiter)
{
vector<string> subStringList;
string token;
while (true)
{
size_t findfirst = mystring.find_first_of(delimiter);
if (findfirst == string::npos) //find_first_of returns npos if it couldn't find the delimiter anymore
{
subStringList.push_back(mystring); //push back the final piece of mystring
return subStringList;
}
token = mystring.substr(0, mystring.find_first_of(delimiter));
mystring = mystring.substr(mystring.find_first_of(delimiter) + 1);
subStringList.push_back(token);
}
return subStringList;
}
还有另一个答案:这里我使用find_first_not_of字符串函数,它返回第一个不匹配delim中指定的任何字符的位置。
size_t find_first_not_of(const string& delim, size_t pos = 0) const noexcept;
例子:
int main()
{
size_t start = 0, end = 0;
std::string str = "scott>=tiger>=cat";
std::string delim = ">=";
while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
{
end = str.find(delim, start); // finds the 'first' occurance from the 'start'
std::cout << str.substr(start, end - start)<<std::endl; // extract substring
}
return 0;
}
输出:
scott
tiger
cat
对于字符串分隔符
基于字符串分隔符拆分字符串。如分割字符串“adsf-+qwret-+nvfkbdsj-+orthdfjgh-+dfjrleih”基于字符串分隔符“-+”,输出将是{“adsf”,“qwret”,“nvfkbdsj”,“orthdfjgh”,“dfjrleih”}
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
// for string delimiter
vector<string> split (string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;
while ((pos_end = s.find (delimiter, pos_start)) != string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}
res.push_back (s.substr (pos_start));
return res;
}
int main() {
string str = "adsf-+qwret-+nvfkbdsj-+orthdfjgh-+dfjrleih";
string delimiter = "-+";
vector<string> v = split (str, delimiter);
for (auto i : v) cout << i << endl;
return 0;
}
**Output**
adsf
qwret
nvfkbdsj
orthdfjgh
dfjrleih
对于单字符分隔符
基于字符分隔符拆分字符串。例如,使用分隔符“+”分割字符串“adsf+qwer+poui+fdgh”将输出{“adsf”,“qwer”,“poui”,“fdgh”}
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<string> split (const string &s, char delim) {
vector<string> result;
stringstream ss (s);
string item;
while (getline (ss, item, delim)) {
result.push_back (item);
}
return result;
}
int main() {
string str = "adsf+qwer+poui+fdgh";
vector<string> v = split (str, '+');
for (auto i : v) cout << i << endl;
return 0;
}
**Output**
adsf
qwer
poui
fdgh
template<typename C, typename T>
auto insert_in_container(C& c, T&& t) -> decltype(c.push_back(std::forward<T>(t)), void()) {
c.push_back(std::forward<T>(t));
}
template<typename C, typename T>
auto insert_in_container(C& c, T&& t) -> decltype(c.insert(std::forward<T>(t)), void()) {
c.insert(std::forward<T>(t));
}
template<typename Container>
Container splitR(const std::string& input, const std::string& delims) {
Container out;
size_t delims_len = delims.size();
auto begIdx = 0u;
auto endIdx = input.find(delims, begIdx);
if (endIdx == std::string::npos && input.size() != 0u) {
insert_in_container(out, input);
}
else {
size_t w = 0;
while (endIdx != std::string::npos) {
w = endIdx - begIdx;
if (w != 0) insert_in_container(out, input.substr(begIdx, w));
begIdx = endIdx + delims_len;
endIdx = input.find(delims, begIdx);
}
w = input.length() - begIdx;
if (w != 0) insert_in_container(out, input.substr(begIdx, w));
}
return out;
}