使用Lamda实现Split,编译通过,未测试

This commit is contained in:
nnhy 2016-03-05 12:15:45 +00:00
parent f78e0e4263
commit 6c55562e70
2 changed files with 30 additions and 0 deletions

View File

@ -790,6 +790,33 @@ bool String::EndsWith(const char* str) const
return strncmp(&buffer[len - slen], str, slen) == 0;
}
int String::Split(const String& str, StringItem callback)
{
if(str.Length() == 0) return 0;
int n = 0;
int p = 0;
int e = 0;
while(p < len)
{
// 找到下一个位置。如果找不到,直接移到末尾
e = IndexOf(str, p);
if(e < 0) e = len;
n++;
auto item = Substring(p, e - p);
callback(item);
// 如果在末尾,说明没有找到
if(e == len) break;
p = e + str.Length();
}
return n;
}
String String::Substring(int start, int length) const
{
String str;

View File

@ -134,6 +134,9 @@ public:
bool StartsWith(const char* str, int startIndex = 0) const;
bool EndsWith(const String& str) const;
bool EndsWith(const char* str) const;
typedef void (*StringItem)(const String& item);
int Split(const String& str, StringItem callback);
String Substring(int start, int len) const;
String& TrimStart();