StringUtils.cpp
1.16 KB
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
//
//  StringUtils.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 02.06.2017.
//
//
#include <stdio.h>
#include "StringUtils.h"
std::vector<std::string> StringUtils::splitString(const std::string& string, char separator)
{
    std::vector<std::string> tokens;
    
    std::string::size_type prev_pos = 0, pos = 0;
    while((pos = string.find(separator, pos)) != std::string::npos)
    {
        std::string substring( string.substr(prev_pos, pos-prev_pos) );
        tokens.push_back(substring);
        prev_pos = ++pos;
    }
    tokens.push_back(string.substr(prev_pos, pos-prev_pos));
    
    return tokens;
}
std::vector<std::string> StringUtils::splitString(const std::string& string, std::string separator)
{
    std::vector<std::string> tokens;
    
    std::string::size_type prev_pos = 0, pos = 0;
    while((pos = string.find(separator, pos)) != std::string::npos)
    {
        std::string substring( string.substr(prev_pos, pos-prev_pos) );
        tokens.push_back(substring);
        prev_pos = pos + separator.length();
        pos = prev_pos;
    }
    tokens.push_back(string.substr(prev_pos, pos-prev_pos));
    
    return tokens;
}