-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplit.cpp
72 lines (63 loc) · 1.47 KB
/
Split.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Common.h"
std::vector<std::string> Split(const std::string& str, char delimiter, int position) {
std::vector<std::string> tokens;
std::string token;
std::stringstream ss(str);
while (std::getline(ss, token, delimiter))
{
if (!token.empty())
{
tokens.push_back(token);
}
}
if (position >= 0 && position < tokens.size())
{
return { tokens[position] };
}
else
{
return tokens;
}
}
std::string GetSplitElement(const std::string& str, char delimiter, int index) {
std::vector<std::string> tokens;
std::regex regex(std::string(1, delimiter) + "+");
std::sregex_token_iterator it(str.begin(), str.end(), regex, -1);
std::sregex_token_iterator end;
while (it != end) {
tokens.push_back(*it);
++it;
}
if (index >= 0 && index < tokens.size()) {
return tokens[index];
}
else {
return ""; // Restituisci una stringa vuota se l'indice non è valido
}
}
std::vector<std::vector<char>> Split2(const std::string& str, char delimiter, int pos) {
std::vector<std::vector<char>> wordList;
std::vector<char> word;
for (char letter : str)
{
if (letter == delimiter)
{
wordList.emplace_back(word);
word.clear();
}
else
{
word.emplace_back(letter);
}
}
wordList.emplace_back(word);
if (pos != -1 && pos < wordList.size()) {
std::vector<std::vector<char>> singleWord;
singleWord.emplace_back(wordList[pos]);
return singleWord;
}
else
{
return wordList;
}
}