return v1parts.size() - v2parts.size();
}
+ string join(const string_list& l, const string& joinWith)
+ {
+ string result;
+ unsigned int count = l.size();
+ for (unsigned int i=0; i < count; ++i) {
+ result += l[i];
+ if (i < (count - 1)) {
+ result += joinWith;
+ }
+ }
+
+ return result;
+ }
} // end namespace strutils
#include <vector>
#include <cstdlib>
+typedef std::vector < std::string > string_list;
namespace simgear {
namespace strutils {
* resulting in at most maxsplit+1 words.
* @return Array of words.
*/
- std::vector<std::string>
+ string_list
split( const std::string& s,
const char* sep = 0,
int maxsplit = 0 );
+ /**
+ * create a single string by joining the elements of a list with
+ * another string.
+ */
+ std::string join(const string_list& l, const std::string& joinWith = "");
+
/**
* Test if a string starts with a string
*
// since we compare numerically, leasing zeros shouldn't matter
VERIFY(compare_versions("0.06.7", "0.6.07") == 0);
+ string_list la = split("zero one two three four five");
+ COMPARE(la[2], "two");
+ COMPARE(la[5], "five");
+ COMPARE(la.size(), 6);
+
+ string_list lb = split("alpha:beta:gamma:delta", ":", 2);
+ COMPARE(lb.size(), 3);
+ COMPARE(lb[0], "alpha");
+ COMPARE(lb[1], "beta");
+ COMPARE(lb[2], "gamma:delta");
+
+ std::string j = join(la, "&");
+ COMPARE(j, "zero&one&two&three&four&five");
+
cout << "all tests passed successfully!" << endl;
return 0;
}