delphi 字符串比较函数

发布时间 2023-06-22 09:47:27作者: txgh

字符串比较函数

列表

方法 说明 大小写
System.SysUtils.TStringHelper.StartsWith 返回是否以给定的字符串开头。 区分大小写
System.SysUtils.TStringHelper.StartsText 返回是否以给定的字符串开头。 不区分大小写
System.SysUtils.TStringHelper.EndsWith 返回是否以给定的字符串结尾。 区分大小写
System.SysUtils.TStringHelper.EndsText 返回是否以给定的字符串结尾。 不区分大小写
System.SysUtils.SameText 按顺序比较两个字符串。 不区分大小写
System.SysUtils.AnsiSameText 根据当前语言环境比较字符串。 不区分大小写
System.SysUtils.SameStr 按顺序比较两个字符串。 区分大小写
System.SysUtils.AnsiSameStr 根据当前语言环境比较字符串。 区分大小写
System.SysUtils.CompareText 比较两个字符串。 区分大小写
System.SysUtils.AnsiCompareText 根据当前语言环境比较字符串。 不区分大小写
System.SysUtils.CompareStr 比较两个字符串。 区分大小写
System.SysUtils.AnsiCompareStr 根据当前语言环境比较字符串。 区分大小写
System.SysUtils.StrComp 比较以 null 结尾的字符串。 区分大小写
System.SysUtils.AnsiStrComp 根据当前语言环境比较以 null 结尾的字符串。 区分大小写
System.SysUtils.StrIComp 比较以 null 结尾的字符串。 不区分大小写
System.SysUtils.AnsiStrIComp 根据当前语言环境比较以 null 结尾的字符串。 不区分大小写
System.SysUtils.StrLComp 比较以 null 结尾的字符串的前 MaxLen 个字节。 区分大小写
System.SysUtils.AnsiStrLComp 根据当前语言环境比较以 null 结尾的字符串的前 MaxLen 个字节。 区分大小写
System.SysUtils.StrLIComp 比较以 null 结尾的字符串的前 MaxLen 个字节。 不区分大小写
System.SysUtils.AnsiStrLIComp 根据当前语言环境比较以 null 结尾的字符串的前 MaxLen 个字节。 不区分大小写

详细

System.SysUtils.TStringHelper.StartsWith

function StartsWith(const Value: string): Boolean;
function StartsWith(const Value: string; IgnoreCase: Boolean): Boolean;

unit

System.SysUtils

返回是否以给定的字符串开头。

参数

Value 给定字符串。

IgnoreCase 是否使用区分大小写。

重载

StartsWith(string) 区分大小写

System.SysUtils.TStringHelper.StartsText

class function StartsText(const ASubText, AText: string): Boolean;

unit

System.SysUtils

返回是否以给定的字符串开头,不区分大小写。

System.SysUtils.TStringHelper.EndsWith

function EndsWith(const Value: string): Boolean;
function EndsWith(const Value: string; IgnoreCase: Boolean): Boolean;

unit

System.SysUtils

返回是否以给定的字符串结尾。

参数

Value 给定字符串。

IgnoreCase 是否使用区分大小写。

重载

EndsWith(string) 区分大小写

System.SysUtils.TStringHelper.EndsText

class function EndsText(const ASubText, AText: string): Boolean;

unit

System.SysUtils

返回是否以给定的字符串结尾,不区分大小写。

System.SysUtils.SameText

function SameText(const S1, S2: string): Boolean;
function SameText(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean;

unit

System.SysUtils

按顺序比较两个字符串,不区分大小写。

System.SysUtils.AnsiSameText

function AnsiSameText(const S1, S2: string): Boolean;

unit

System.SysUtils

根据当前语言环境比较字符串,不区分大小写。

System.SysUtils.SameStr

function SameStr(const S1, S2: string): Boolean;
function SameStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean;

unit

System.SysUtils

按顺序比较两个字符串,区分大小写。

System.SysUtils.AnsiSameStr

function AnsiSameStr(const S1, S2: string): Boolean;

unit

System.SysUtils

根据当前语言环境比较字符串,区分大小写。

System.SysUtils.CompareText

function CompareText(const S1, S2: string): Integer;
function CompareText(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer;

unit

System.SysUtils

按顺序比较两个字符串,不区分大小写。

返回值

如果 S1 = S2 ,则返回 0。如果 S1 > S2,则返回大于 0 的整数。如果 S1 < S2,则返回小于 0 的整数。

System.SysUtils.AnsiCompareText

function AnsiCompareText(const S1, S2: string): Integer;

unit

System.SysUtils

根据当前语言环境比较字符串,不区分大小写。

返回值

如果 S1 = S2 ,则返回 0。如果 S1 > S2,则返回大于 0 的整数。如果 S1 < S2,则返回小于 0 的整数。

System.SysUtils.CompareStr

function CompareStr(const S1, S2: string): Integer;
function CompareStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer;

unit

System.SysUtils

比较两个字符串,区分大小写。

返回值

如果 S1 = S2 ,则返回 0。如果 S1 > S2,则返回大于 0 的整数。如果 S1 < S2,则返回小于 0 的整数。

System.SysUtils.AnsiCompareStr

function AnsiCompareStr(const S1, S2: string): Integer;

unit

System.SysUtils

根据当前语言环境比较字符串,区分大小写。

返回值

如果 S1 = S2 ,则返回 0。如果 S1 > S2,则返回大于 0 的整数。如果 S1 < S2,则返回小于 0 的整数。

大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。因此,将 S1 设置为'a'并将 S2 设置为 'A' 会导致 AnsiCompareStr 返回一个小于0的值,而具有相同参数的 CompareStr 返回一个大于0的值。

System.SysUtils.StrComp

function StrComp(const Str1, Str2: PAnsiChar): Integer;
function StrComp(const Str1, Str2: PWideChar): Integer;

比较以 null 结尾的字符串,区分大小写。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

System.SysUtils.AnsiStrComp

function AnsiStrComp(S1, S2: PAnsiChar): Integer;
function AnsiStrComp(S1, S2: PWideChar): Integer;

unit

System.SysUtils

根据当前语言环境比较以 null 结尾的字符串,区分大小写。

返回值

如果 S1 = S2 ,则返回 0。如果 S1 > S2,则返回大于 0 的整数。如果 S1 < S2,则返回小于 0 的整数。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。

System.SysUtils.StrIComp

function StrIComp(const Str1, Str2: PAnsiChar): Integer;
function StrIComp(const Str1, Str2: PWideChar): Integer;

unit

System.SysUtils

比较以 null 结尾的字符串,不区分大小写。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

System.SysUtils.AnsiStrIComp

function AnsiStrIComp(S1, S2: PAnsiChar): Integer;
function AnsiStrIComp(S1, S2: PWideChar): Integer;

unit

System.SysUtils

根据当前语言环境比较以 null 结尾的字符串,不区分大小写。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

System.SysUtils.StrLComp

function StrLComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
function StrLComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;

unit

System.SysUtils

比较以 null 结尾的字符串的前 MaxLen 个字节,区分大小写。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

System.SysUtils.AnsiStrLComp

function AnsiStrLComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
function AnsiStrLComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer;

unit

System.SysUtils

根据当前语言环境比较以 null 结尾的字符串的前 MaxLen 个字节,区分大小写。

如果 S1 和 S2 包含空字符,并且空字符出现在MaxLen字节之前,AnsiStrLComp 可以在空字符结束后继续比较。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。

System.SysUtils.StrLIComp

function StrLIComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
function StrLIComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;

unit

System.SysUtils

比较以 null 结尾的字符串的前 MaxLen 个字节,不区分大小写。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

System.SysUtils.AnsiStrLIComp

function AnsiStrLIComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
function AnsiStrLIComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer;

unit

System.SysUtils

根据当前语言环境比较以 null 结尾的字符串的前 MaxLen 个字节,不区分大小写。

如果 S1 和 S2 包含空字符,并且空字符出现在MaxLen字节之前,AnsiStrLIComp 可以在空字符结束后继续比较。

System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。

大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。

System.SysUtils.TLocaleOptions

TLocaleOptions = (loInvariantLocale, loUserLocale);

unit

System.SysUtils

定义了依赖和独立语言环境选项的选择。这种特定于 Windows 平台类型在许多字符串处理函数中使用,其中区域设置语言可能对函数有影响。

  • loInvariantLocale 在需要一致的应用程序间操作、独立于实际语言环境的情况下使用此选项。
  • loUserLocale 在考虑用户区域设置的情况下使用此选项。

在 OSX、iOS 和 Android 平台上,如果我们使用 loUserLocale 作为 LocaleOptions 参数的值,所有控制字符(字符代码在#31和#00之间)在比较中被视为相同的字符。

示例

TStringHelper.StartsWith和TStringHelper.StartsText

procedure TForm1.Button1Click(Sender: TObject);
var
  str: string;
begin
  str := 'Abc123456';
  if not str.StartsWith('abc') then
    Memo1.Lines.Add('开头不匹配,StartsWith区分大小写');

  if str.StartsWith('abc', True) then
    Memo1.Lines.Add('开头匹配,StartsWith参数IgnoreCase为True,不区分大小写');

  if string.StartsText('abc', str) then
    Memo1.Lines.Add('开头匹配,StartsText不区分大小写');
end;

TStringHelper.EndsWith和TStringHelper.EndsText

procedure TForm1.Button2Click(Sender: TObject);
var
  str: string;
begin
  str := '123456Abc';
  if not str.EndsWith('abc') then
    Memo1.Lines.Add('结尾不匹配,EndsWith区分大小写');

  if str.EndsWith('abc', True) then
    Memo1.Lines.Add('结尾匹配,EndsWith参数IgnoreCase为True,不区分大小写');

  if string.EndsText('abc', str) then
    Memo1.Lines.Add('结尾匹配,EndsText不区分大小写');
end;

SameText、AnsiSameText、SameStr、AnsiSameStr

procedure TForm1.Button3Click(Sender: TObject);
begin
  if SameText('abc', 'Abc') then
    Memo1.Lines.Add('相等,SameText不区分大小写');

  if AnsiSameText('abc', 'Abc') then
    Memo1.Lines.Add('相等,AnsiSameText不区分大小写');

  if not SameStr('abc', 'Abc') then
    Memo1.Lines.Add('不相等,SameStr区分大小写');

  if not AnsiSameStr('abc', 'Abc') then
    Memo1.Lines.Add('不相等,AnsiSameStr区分大小写');
end;

CompareText、AnsiCompareText、CompareStr、AnsiCompareStr

procedure TForm1.Button4Click(Sender: TObject);
begin
  //判断相等
  if CompareText('abc', 'Abc') = 0 then
    Memo1.Lines.Add('相等,CompareText不区分大小写');
  if AnsiCompareText('abc', 'Abc') = 0 then
    Memo1.Lines.Add('相等,AnsiCompareText不区分大小写');
  if CompareStr('abc', 'Abc') <> 0 then
    Memo1.Lines.Add('不相等,CompareStr区分大小写');
  if AnsiCompareStr('abc', 'Abc') <> 0 then
    Memo1.Lines.Add('不相等,AnsiCompareStr区分大小写');

  //比较大小
  if CompareText('abc12', 'Abc') > 0 then
    Memo1.Lines.Add('左边 大于 右边,第4个字符,左边字符比右边多');
  if CompareText('abc12', 'Abc2') < 0 then
    Memo1.Lines.Add('左边 小于 右边,第4个字符,右边“2”比“1”大');

  //语言环境,CompareStr与AnsiCompareStr区别
  if CompareStr('a', 'A') > 0 then
    Memo1.Lines.Add('CompareStr根据ASCII顺序,小写字符大于大写字符');
  if AnsiCompareStr('a', 'A') < 0 then
    Memo1.Lines.Add('AnsiCompareStr根据语言环境,小写字符小于大写字符');
end;

StrComp和StrIComp

procedure TForm1.Button5Click(Sender: TObject);
var
  p1, p2: PWideChar;
  p3, p4: PAnsiChar;
begin
  p1 := 'abc';
  p2 := 'Abc';
  p3 := 'abc';
  p4 := 'Abc';

  if StrComp(p1, p2) <> 0 then
    Memo1.Lines.Add('不相等,StrComp区分大小写');
  if StrIComp(p1, p2) = 0 then
    Memo1.Lines.Add('相等,StrIComp不区分大小写');

  //比较PAnsiChar使用System.AnsiStrings单元的, System.SysUtils的单元已弃用
  if System.SysUtils.StrComp(p3, p4) <> 0 then
    Memo1.Lines.Add('System.SysUtils.StrComp(PAnsiChar;PAnsiChar)已弃用');
  if System.AnsiStrings.StrComp(p3, p4) <> 0 then
    Memo1.Lines.Add('使用System.AnsiStrings.StrComp(PAnsiChar;PAnsiChar)');
end;

TLocaleOptions

procedure TForm1.Button6Click(Sender: TObject);
var
  s1, s2: string;
begin
  s1 := char(6);
  s2 := char(31);
  if SameText(s1, s2, loUserLocale) then
    Memo1.Lines.Add('相等(OSX、iOS 和 Android 平台)')
  else
    Memo1.Lines.Add('不相等(Windows 平台)');

  if SameText(s1, s2, loInvariantLocale) then
    Memo1.Lines.Add('相等')
  else
    Memo1.Lines.Add('不相等(Windows、OSX、iOS 和 Android 平台)');
end;

返回结果

不相等(Windows 平台)
不相等(Windows、OSX、iOS 和 Android 平台)