使用Lamda实现Split,编译通过,未测试
This commit is contained in:
parent
f78e0e4263
commit
6c55562e70
27
String.cpp
27
String.cpp
|
@ -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;
|
||||
|
|
3
String.h
3
String.h
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue