Created
December 16, 2020 22:18
-
-
Save GiullianoRossi1987/3dda4eaeb4282483fa954285b37d43f9 to your computer and use it in GitHub Desktop.
A Simple split function for strings/char[] c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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