Merge pull request #3146 from tks2shimizu/develop

closed 2408: Add componentsSeparatedByString function for String class.
This commit is contained in:
James Chen 2013-07-15 23:00:12 -07:00
commit 2ed5a40a36
2 changed files with 26 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include "ccMacros.h" #include "ccMacros.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "CCArray.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -151,6 +152,28 @@ void String::appendWithFormat(const char* format, ...)
} }
Array* String::componentsSeparatedByString(const char *delimiter)
{
Array* result = Array::create();
int cutAt;
while( (cutAt = _string.find_first_of(delimiter)) != _string.npos )
{
if(cutAt > 0)
{
result->addObject(String::create(_string.substr(0, cutAt)));
}
_string = _string.substr(cutAt + 1);
}
if(_string.length() > 0)
{
result->addObject(String::create(_string));
}
return result;
}
bool String::isEqual(const Object* pObject) bool String::isEqual(const Object* pObject)
{ {
bool bRet = false; bool bRet = false;

View File

@ -86,6 +86,9 @@ public:
/** append(w/ format) additional characters at the end of its current value */ /** append(w/ format) additional characters at the end of its current value */
void appendWithFormat(const char* format, ...); void appendWithFormat(const char* format, ...);
/** split a string */
Array* componentsSeparatedByString(const char *delimiter);
/* override functions */ /* override functions */
virtual bool isEqual(const Object* pObject); virtual bool isEqual(const Object* pObject);