相信字符串处理中用的最多的就是 pos 函数了.但是如果要搜索一个字符串中第二次或者第三次出现的子字符串的,就没有现成的 delphi 标准函数了.所以我就自己写了一个.同时与网上比较流行的 faststrings.smartpos() 与 jvcl.npos() 做了比较,速度更快,而且兼容 unicode(widestring/widechar).
【程序编程相关:一个定时网络唤醒的Windows 200】 【推荐阅读:Delphi单元文件详解】注:代码可能有人会觉得不太舒服,但作为最常用的字符串函数,这样的优化我觉得还是值得的. 【扩展信息:Delphi下用WindowsAPI创建】 function quickpos(const substr, s: widestring; matchesindex: integer = 1): integer; function quickposback(const substr, s: widestring; matchesreverseindex: integer = 1): integer;代码如下:
// compares a substring with a string. *for inline use" // c: 2004-07-05 | m: 2004-07-05 function _inlinecomparetext(const substr, s: widestring; startindex: integer = 1; lenofsubstr: integer = -1; lenofs: integer = -1): boolean; var ? i: integer; begin ? if lenofsubstr = -1 then lenofsubstr := length(substr); ? if lenofs = -1 then lenofs := length(s); ? if lenofsubstr > lenofs then ? begin ? ? result := false; ? ? exit; ? end; ? for i := 1 to lenofsubstr do ? ? if substr[i] s[i + startindex - 1] then ... 下一页