Design Pattern Examples
Overview of object-oriented design patterns
split.cpp
Go to the documentation of this file.
1
5
6#include <cstring>
7#include "split.h"
8
9namespace Helpers
10{
11
12 // Split the string into multiple strings.
13 std::vector<std::string> split(const char* pszString, const char* splitChars/* = " "*/)
14 {
15 std::vector<std::string> items;
16 if (splitChars == NULL || *splitChars == '\0')
17 {
18 splitChars = " ";
19 }
20
21 const char* workPtr = pszString;
22 for (;;)
23 {
24 size_t foundIndex = strcspn(workPtr, splitChars);
25 if (foundIndex < strlen(workPtr))
26 {
27 items.push_back(std::string(workPtr, 0, foundIndex));
28 workPtr += foundIndex + 1;
29 }
30 else
31 {
32 items.push_back(std::string(workPtr));
33 break;
34 }
35 }
36 return items;
37 }
38
39 std::vector<std::string> split(const std::string& szString,
40 const std::string& splitChars/* = " "*/)
41 {
42 return split(szString.c_str(), splitChars.c_str());
43 }
44
45} // end namespace
Declaration of the split functions, for splitting a string on delimiters.
The namespace containing all the "helper" functions in the C++ code.
std::vector< std::string > split(const char *pszString, const char *splitChars)
Split the given string into a list of strings given the character on which to split....
Definition: split.cpp:13