zoukankan      html  css  js  c++  java
  • cocos2d-x 2.2.6 之 split分割字符串

    如果有这样的数据,如: “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);
        }
    }        
  • 相关阅读:
    OSPF Configuration Examples
    enabling ip forwarding
    LeetCode 153. Find Minimum in Rotated Sorted Array
    洛谷 P1059 明明的随机数
    LeetCode 120. Triangle
    洛谷 P1047 校门外的树(待完善)
    C++万能头文件<bits/stdc++.h>的内容与优缺点
    LeetCode 217. Contains Duplicate
    LeetCode 414. Third Maximum Number
    洛谷 P1540 机器翻译
  • 原文地址:https://www.cnblogs.com/SkyflyBird/p/5117631.html
Copyright © 2011-2022 走看看