Skip to content

Latest commit

 

History

History
26 lines (23 loc) · 586 Bytes

split-string.md

File metadata and controls

26 lines (23 loc) · 586 Bytes
title description author tags
Split String
Splits a string by a delimiter
saminjay
string,split
#include <string>
#include <vector>

std::vector<std::string> split_string(std::string str, std::string delim) {
    std::vector<std::string> splits;
    int i = 0, j;
    int inc = delim.length();
    while (j != std::string::npos) {
        j = str.find(delim, i);
        splits.push_back(str.substr(i, j - i));
        i = j + inc;
    }
    return splits;
}

// Usage:
split_string("quick_-snip", "_-"); // Returns: std::vector<std::string> { "quick", "snip" }