Add split string function #4933

This commit is contained in:
Jerry (Xinyu Hou) 2015-10-29 12:52:55 -07:00
parent 27f83e1801
commit 72397137c0
2 changed files with 27 additions and 0 deletions

View File

@ -224,6 +224,26 @@ stringToSizeType(String string)
return value; return value;
} }
std::vector<String>
splitString(String string, const char c)
{
std::vector<String> results;
size_t head = 0;
size_t separator = string.find(c);
while (separator != String::npos) {
results.push_back(string.substr(head, separator - head));
head = separator + 1;
separator = string.find(c, head);
}
if (head < string.size()) {
results.push_back(string.substr(head, string.size() - head));
}
return results;
}
// //
// CaselessCmp // CaselessCmp
// //

View File

@ -22,6 +22,7 @@
#include "common/stdstring.h" #include "common/stdstring.h"
#include <stdarg.h> #include <stdarg.h>
#include <vector>
// use standard C++ string class for our string class // use standard C++ string class for our string class
typedef std::string String; typedef std::string String;
@ -100,6 +101,12 @@ Convert an a \c string to an size type
*/ */
size_t stringToSizeType(String string); size_t stringToSizeType(String string);
//! Split a string into substrings
/*!
Split a \c string that separated by a \c c into substrings
*/
std::vector<String> splitString(String string, const char c);
//! Case-insensitive comparisons //! Case-insensitive comparisons
/*! /*!
This class provides case-insensitve comparison functions. This class provides case-insensitve comparison functions.