有没有在delphi内置函数来查找字符串是否包含子,但不是在结束了吗?
例如,假设我有这些字符串:
G15001, G15005, G15015, G14015, G14004, PLU15010, PLU14015
我想在字符串为G15001 G15005,G15015,PLU15010和搜索的子字符串为15时返回true,但在G14015或PLU14015时返回false,因为它们最后只有15.
使用Pos检查,如果子都可以找到.然后检查子串是否位于末尾.
function ContainsBeforeEnd(const str, substr: string): Boolean;
var
P: Integer;
begin
P := Pos(substr, str);
if P = 0 then
// substr not found at all
Result := False
else
// found, now check whether substr is at the end of str
Result := P + Length(substr) - 1 <> Length(str);
end;
Run Code Online (Sandbox Code Playgroud)