Skip to content

Instantly share code, notes, and snippets.

@GiullianoRossi1987
Created December 16, 2020 22:18
Show Gist options
  • Save GiullianoRossi1987/3dda4eaeb4282483fa954285b37d43f9 to your computer and use it in GitHub Desktop.
Save GiullianoRossi1987/3dda4eaeb4282483fa954285b37d43f9 to your computer and use it in GitHub Desktop.
A Simple split function for strings/char[] c++
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
std::vector<std::string> split(std::string str, char delimiter){
std::vector<std::string> result;
// std::cout << str << std::endl;
std::string buffer = "";
// int splittedCounter = 0;
for(int i=0; i < str.size(); i++){
char letter = str[i];
// std::cout << letter << std::endl;
if(letter != delimiter){
buffer += letter;
}
else{
// std::cout << buffer << std::endl;
result.push_back(buffer);
// splittedCounter++;
buffer = "";
}
}
// std::cout << std::to_string(result) << std::endl;
if(buffer.size() > 0) result.push_back(buffer);
return result;
}
int main(int argc, char** argv){
std::string data = "testando/123";
std::vector<std::string> rt = splitString(data, '/');
int counter = 0;
for(int i = 0; i < rt.size(); i++){
std::cout << "Pos " + std::to_string(i) + " ";
std::cout << rt[i] << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment