如果有这样的数据,如: “100#200#300#400”,在之前使用orge引擎的情况下,可借助split来实现,如:
Ogre::String str = "100#200#300#400"; StringVector strVec = StringUtil::split(str,"#");
其实现代码如下,在OgreString.cpp中:
// 头文件 static vector<String>::type split( const String& str, const String& delims = " ", unsigned int maxSplits = 0); // 实现文件 StringVector StringUtil::split( const String& str, const String& delims, unsigned int maxSplits) { StringVector ret; // Pre-allocate some space for performance ret.reserve(maxSplits ? maxSplits+1 : 10); // 10 is guessed capacity for most case unsigned int numSplits = 0; // Use STL methods size_t start, pos; start = 0; do { pos = str.find_first_of(delims, start); if (pos == start) { // Do nothing start = pos + 1; } else if (pos == String::npos || (maxSplits && numSplits == maxSplits)) { // Copy the rest of the string ret.push_back( str.substr(start) ); break; } else { // Copy up to delimiter ret.push_back( str.substr(start, pos - start) ); start = pos + 1; } // parse up to next real data start = str.find_first_not_of(delims, start); ++numSplits; } while (pos != String::npos); return ret; }
那么cocos2d-x 中有类似的函数存在吗?很幸运还是找到了,大家可参考:...cocoaCCNS.cpp,其实现代码如下:
#include "CCNS.h" // typedef std::vector<std::string> strArray; // string toolkit static inline void split(std::string src, const char* token, strArray& vect) { int nend=0; int nbegin=0; while(nend != -1) { nend = src.find(token, nbegin); if(nend == -1) vect.push_back(src.substr(nbegin, src.length()-nbegin)); else vect.push_back(src.substr(nbegin, nend-nbegin)); nbegin = nend + strlen(token); } }