How to include partial string?

closed account (Ey80oG1T)
So let's say that I have a string:

 
string name = "Dynamic_Squid"


How do I separate that string into two parts?

 
"Dynamic" and "Squid"


So in this case, I separated the string based on the "_" character, but it can be any character. How do I do this? Thanks.
Last edited on
The requirements you're describing are unclear.

"Any character" --> what should "DynamicXSquid" be split into?
What about "YDynamicYSquid"? "__Dynamic__Squid__"?

The actual answer to your question probably involves the use of string.find and string.substr.
http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/substr/
Last edited on
I assume you mean it can be any specified character. If so, something like this:
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
// Example program
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string>
string_split(const std::string& str, char delimiter)
{
    std::vector<std::string> split;
    size_t pos = 0;
    size_t index;
    while ((index = str.find(delimiter, pos)) != std::string::npos)
    {
        size_t length = index - pos;
        split.push_back(str.substr(pos, length));
        pos = index + 1;
    }
    
    if (pos < str.length() - 1)
    {
    	split.push_back(str.substr(pos));	
    }

    return split;
}


int main()
{
    auto split = string_split("Dynamic_Squid_Another_Token", '_');
    
    for (const std::string& str : split)
    {
        std::cout << str << '\n';
    }
}
here is a better version
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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void split(const string &str, const char delimiter, vector<string> &result)
{
    string word;
    for (size_t i = 0; i < str.length(); ++i)
    {
        if (str[i] == delimiter)
        {
            result.push_back(word);
            word = "";
        }
        else
            word += str[i];
    }
    if (str.size())
        result.push_back(word);
}

int main()
{
    string name = "Hello_World";
    
    vector<string> result;
    
    split(name, '_', result);
    
    for (string &s : result)
        cout << s << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
   string text = "Dynamic_Squid_Another_Token";
   stringstream ss( text );
   string token;
   while( getline( ss, token, '_' ) ) cout << token << '\n';
}
Split using an arbitrary regular expression:
1
2
3
4
std::vector<std::string> split_re(std::string_view sv, std::regex const& re)
{
  return {std::regex_token_iterator(sv.begin(), sv.end(), re, -1), {}};
}

http://coliru.stacked-crooked.com/a/e89189a9754a396a
Last edited on
@mbozzi Very elegant solution. I totally forgot that you can iterate over regular expressions
Last edited on
Topic archived. No new replies allowed.