Implementation Split
This commit is contained in:
parent
5f791b8d9f
commit
25bd269729
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,7 +5,7 @@
|
|||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
# CMake
|
# CMake
|
||||||
cmake-build-*/
|
*build*/
|
||||||
|
|
||||||
# Mongo Explorer plugin
|
# Mongo Explorer plugin
|
||||||
.idea/**/mongoSettings.xml
|
.idea/**/mongoSettings.xml
|
||||||
|
@ -4,7 +4,11 @@ project(Exec)
|
|||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
include_directories(../Src)
|
include_directories(../Src)
|
||||||
link_directories(../Src/cmake-build-debug)
|
|
||||||
|
|
||||||
add_executable(Exec main.cpp)
|
FILE(GLOB StringTools ../Src/*.cpp)
|
||||||
target_link_libraries(Exec StringTools)
|
|
||||||
|
add_executable(Exec
|
||||||
|
${StringTools}
|
||||||
|
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
@ -3,7 +3,11 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << StringTools::Replace("Hello, ${where}!\n", "${where}", "World") << std::endl;
|
std::vector<std::string> foo =
|
||||||
|
StringTools::Split("Hello, lol, test", ", ");
|
||||||
|
|
||||||
|
for (const auto& it : foo)
|
||||||
|
std::cout << "'" << it << "'" << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -92,3 +92,31 @@ std::string StringTools::Upper(const std::string& str) {
|
|||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
|
||||||
|
std::vector<std::string> toRet;
|
||||||
|
|
||||||
|
// Quick-accept: seperator length is 0
|
||||||
|
if (seperator.length() == 0) {
|
||||||
|
for (const char c : str)
|
||||||
|
toRet.push_back(std::string(&c, (&c) + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
std::size_t idx = 0;
|
||||||
|
while (idx != std::string::npos) {
|
||||||
|
std::size_t lastIdx = idx;
|
||||||
|
idx = str.find(seperator, idx + seperator.length());
|
||||||
|
|
||||||
|
toRet.push_back(str.substr(
|
||||||
|
lastIdx,
|
||||||
|
idx - lastIdx
|
||||||
|
));
|
||||||
|
|
||||||
|
if (idx != std::string::npos)
|
||||||
|
idx += seperator.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toRet;
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define STRINGTOOLS_STRINGTOOLS_H
|
#define STRINGTOOLS_STRINGTOOLS_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
/* Handy utensils to manipulate strings */
|
/* Handy utensils to manipulate strings */
|
||||||
class StringTools
|
class StringTools
|
||||||
@ -25,6 +26,9 @@ public:
|
|||||||
//! Will make a string all-uppercase.
|
//! Will make a string all-uppercase.
|
||||||
static std::string Upper(const std::string& str);
|
static std::string Upper(const std::string& str);
|
||||||
|
|
||||||
|
//! Will split a string by a string seperator
|
||||||
|
static std::vector<std::string> Split(const std::string& str, const std::string& seperator);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// No instanciation! >:(
|
// No instanciation! >:(
|
||||||
StringTools();
|
StringTools();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user