delphi 字符串比较函数
<h1 id="字符串比较函数">字符串比较函数</h1><h2 id="列表">列表</h2>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
<th>大小写</th>
</tr>
</thead>
<tbody>
<tr>
<td>System.SysUtils.TStringHelper.StartsWith</td>
<td>返回是否以给定的字符串开头。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.TStringHelper.StartsText</td>
<td>返回是否以给定的字符串开头。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.TStringHelper.EndsWith</td>
<td>返回是否以给定的字符串结尾。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.TStringHelper.EndsText</td>
<td>返回是否以给定的字符串结尾。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.SameText</td>
<td>按顺序比较两个字符串。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiSameText</td>
<td>根据当前语言环境比较字符串。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.SameStr</td>
<td>按顺序比较两个字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiSameStr</td>
<td>根据当前语言环境比较字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.CompareText</td>
<td>比较两个字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiCompareText</td>
<td>根据当前语言环境比较字符串。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.CompareStr</td>
<td>比较两个字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiCompareStr</td>
<td>根据当前语言环境比较字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.StrComp</td>
<td>比较以 null 结尾的字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiStrComp</td>
<td>根据当前语言环境比较以 null 结尾的字符串。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.StrIComp</td>
<td>比较以 null 结尾的字符串。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiStrIComp</td>
<td>根据当前语言环境比较以 null 结尾的字符串。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.StrLComp</td>
<td>比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiStrLComp</td>
<td>根据当前语言环境比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节。</td>
<td>区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.StrLIComp</td>
<td>比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节。</td>
<td>不区分大小写</td>
</tr>
<tr>
<td>System.SysUtils.AnsiStrLIComp</td>
<td>根据当前语言环境比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节。</td>
<td>不区分大小写</td>
</tr>
</tbody>
</table>
<h2 id="详细">详细</h2>
<h3 id="systemsysutilststringhelperstartswith">System.SysUtils.TStringHelper.StartsWith</h3>
<pre><code class="language-delphi">function StartsWith(const Value: string): Boolean;
function StartsWith(const Value: string; IgnoreCase: Boolean): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>返回是否以给定的字符串开头。</p>
<p><strong>参数</strong></p>
<p><em>Value</em> 给定字符串。</p>
<p><em>IgnoreCase</em> 是否使用区分大小写。</p>
<p><strong>重载</strong></p>
<p><code>StartsWith(string)</code> 区分大小写</p>
<h3 id="systemsysutilststringhelperstartstext">System.SysUtils.TStringHelper.StartsText</h3>
<pre><code class="language-delphi">class function StartsText(const ASubText, AText: string): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>返回是否以给定的字符串开头,不区分大小写。</p>
<h3 id="systemsysutilststringhelperendswith">System.SysUtils.TStringHelper.EndsWith</h3>
<pre><code class="language-delphi">function EndsWith(const Value: string): Boolean;
function EndsWith(const Value: string; IgnoreCase: Boolean): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>返回是否以给定的字符串结尾。</p>
<p><strong>参数</strong></p>
<p><em>Value</em> 给定字符串。</p>
<p><em>IgnoreCase</em> 是否使用区分大小写。</p>
<p><strong>重载</strong></p>
<p><code>EndsWith(string)</code> 区分大小写</p>
<h3 id="systemsysutilststringhelperendstext">System.SysUtils.TStringHelper.EndsText</h3>
<pre><code class="language-delphi">class function EndsText(const ASubText, AText: string): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>返回是否以给定的字符串结尾,不区分大小写。</p>
<h3 id="systemsysutilssametext">System.SysUtils.SameText</h3>
<pre><code class="language-delphi">function SameText(const S1, S2: string): Boolean;
function SameText(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>按顺序比较两个字符串,不区分大小写。</p>
<h3 id="systemsysutilsansisametext">System.SysUtils.AnsiSameText</h3>
<pre><code class="language-delphi">function AnsiSameText(const S1, S2: string): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较字符串,不区分大小写。</p>
<h3 id="systemsysutilssamestr">System.SysUtils.SameStr</h3>
<pre><code class="language-delphi">function SameStr(const S1, S2: string): Boolean;
function SameStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>按顺序比较两个字符串,区分大小写。</p>
<h3 id="systemsysutilsansisamestr">System.SysUtils.AnsiSameStr</h3>
<pre><code class="language-delphi">function AnsiSameStr(const S1, S2: string): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较字符串,区分大小写。</p>
<h3 id="systemsysutilscomparetext">System.SysUtils.CompareText</h3>
<pre><code class="language-delphi">function CompareText(const S1, S2: string): Integer;
function CompareText(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>按顺序比较两个字符串,不区分大小写。</p>
<p><strong>返回值</strong></p>
<p>如果 <em>S1</em> = <em>S2</em> ,则返回 0。如果 <code>S1</code> > <code>S2</code>,则返回大于 0 的整数。如果 <code>S1</code> < <code>S2</code>,则返回小于 0 的整数。</p>
<h3 id="systemsysutilsansicomparetext">System.SysUtils.AnsiCompareText</h3>
<pre><code class="language-delphi">function AnsiCompareText(const S1, S2: string): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较字符串,不区分大小写。</p>
<p><strong>返回值</strong></p>
<p>如果 <em>S1</em> = <em>S2</em> ,则返回 0。如果 <code>S1</code> > <code>S2</code>,则返回大于 0 的整数。如果 <code>S1</code> < <code>S2</code>,则返回小于 0 的整数。</p>
<h3 id="systemsysutilscomparestr">System.SysUtils.CompareStr</h3>
<pre><code class="language-delphi">function CompareStr(const S1, S2: string): Integer;
function CompareStr(const S1, S2: string; LocaleOptions: TLocaleOptions): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>比较两个字符串,区分大小写。</p>
<p><strong>返回值</strong></p>
<p>如果 <em>S1</em> = <em>S2</em> ,则返回 0。如果 <code>S1</code> > <code>S2</code>,则返回大于 0 的整数。如果 <code>S1</code> < <code>S2</code>,则返回小于 0 的整数。</p>
<h3 id="systemsysutilsansicomparestr">System.SysUtils.AnsiCompareStr</h3>
<pre><code class="language-delphi">function AnsiCompareStr(const S1, S2: string): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较字符串,区分大小写。</p>
<p><strong>返回值</strong></p>
<p>如果 <em>S1</em> = <em>S2</em> ,则返回 0。如果 <code>S1</code> > <code>S2</code>,则返回大于 0 的整数。如果 <code>S1</code> < <code>S2</code>,则返回小于 0 的整数。</p>
<blockquote>
<p>大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。因此,将 <em>S1</em> 设置为<code>'a'</code>并将 S2 设置为 <code>'A'</code> 会导致 <code>AnsiCompareStr</code> 返回一个小于0的值,而具有相同参数的 <code>CompareStr</code> 返回一个大于0的值。</p>
</blockquote>
<h3 id="systemsysutilsstrcomp">System.SysUtils.StrComp</h3>
<pre><code class="language-delphi">function StrComp(const Str1, Str2: PAnsiChar): Integer;
function StrComp(const Str1, Str2: PWideChar): Integer;
</code></pre>
<p>比较以 null 结尾的字符串,区分大小写。</p>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
</blockquote>
<h3 id="systemsysutilsansistrcomp">System.SysUtils.AnsiStrComp</h3>
<pre><code class="language-delphi">function AnsiStrComp(S1, S2: PAnsiChar): Integer;
function AnsiStrComp(S1, S2: PWideChar): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较以 null 结尾的字符串,区分大小写。</p>
<p><strong>返回值</strong></p>
<p>如果 <em>S1</em> = <em>S2</em> ,则返回 0。如果 <code>S1</code> > <code>S2</code>,则返回大于 0 的整数。如果 <code>S1</code> < <code>S2</code>,则返回小于 0 的整数。</p>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
<p>大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。</p>
</blockquote>
<h3 id="systemsysutilsstricomp">System.SysUtils.StrIComp</h3>
<pre><code class="language-delphi">function StrIComp(const Str1, Str2: PAnsiChar): Integer;
function StrIComp(const Str1, Str2: PWideChar): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>比较以 null 结尾的字符串,不区分大小写。</p>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
</blockquote>
<h3 id="systemsysutilsansistricomp">System.SysUtils.AnsiStrIComp</h3>
<pre><code class="language-delphi">function AnsiStrIComp(S1, S2: PAnsiChar): Integer;
function AnsiStrIComp(S1, S2: PWideChar): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较以 null 结尾的字符串,不区分大小写。</p>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
</blockquote>
<h3 id="systemsysutilsstrlcomp">System.SysUtils.StrLComp</h3>
<pre><code class="language-delphi">function StrLComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
function StrLComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节,区分大小写。</p>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
</blockquote>
<h3 id="systemsysutilsansistrlcomp">System.SysUtils.AnsiStrLComp</h3>
<pre><code class="language-delphi">function AnsiStrLComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
function AnsiStrLComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节,区分大小写。</p>
<blockquote>
<p>如果 S1 和 S2 包含空字符,并且空字符出现在<em>MaxLen</em>字节之前,<code>AnsiStrLComp</code> 可以在空字符结束后继续比较。</p>
</blockquote>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
<p>大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。</p>
</blockquote>
<h3 id="systemsysutilsstrlicomp">System.SysUtils.StrLIComp</h3>
<pre><code class="language-delphi">function StrLIComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
function StrLIComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节,不区分大小写。</p>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
</blockquote>
<h3 id="systemsysutilsansistrlicomp">System.SysUtils.AnsiStrLIComp</h3>
<pre><code class="language-delphi">function AnsiStrLIComp(S1, S2: PAnsiChar; MaxLen: Cardinal): Integer;
function AnsiStrLIComp(S1, S2: PWideChar; MaxLen: Cardinal): Integer;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>根据当前语言环境比较以 null 结尾的字符串的前 <em>MaxLen</em> 个字节,不区分大小写。</p>
<blockquote>
<p>如果 S1 和 S2 包含空字符,并且空字符出现在<em>MaxLen</em>字节之前,<code>AnsiStrLIComp</code> 可以在空字符结束后继续比较。</p>
</blockquote>
<blockquote>
<p>System.SysUtils 单元的 ANSI 版本已弃用。请使用 System.AnsiStrings 单元。</p>
<p>大多数语言环境认为小写字符小于对应的大写字符。这与 ASCII 顺序相反,在 ASCII 顺序中,小写字符大于大写字符。</p>
</blockquote>
<h3 id="systemsysutilstlocaleoptions">System.SysUtils.TLocaleOptions</h3>
<pre><code class="language-delphi">TLocaleOptions = (loInvariantLocale, loUserLocale);
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>定义了依赖和独立语言环境选项的选择。这种特定于 Windows 平台类型在许多字符串处理函数中使用,其中区域设置语言可能对函数有影响。</p>
<ul>
<li><em>loInvariantLocale</em>在需要一致的应用程序间操作、独立于实际语言环境的情况下使用此选项。</li>
<li><em>loUserLocale</em>在考虑用户区域设置的情况下使用此选项。</li>
</ul>
<blockquote>
<p>在 OSX、iOS 和 Android 平台上,如果我们使用 <code>loUserLocale</code> 作为 <em>LocaleOptions</em> 参数的值,所有控制字符(字符代码在#31和#00之间)在比较中被视为相同的字符。</p>
</blockquote>
<h2 id="示例">示例</h2>
<h3 id="tstringhelperstartswith和tstringhelperstartstext">TStringHelper.StartsWith和TStringHelper.StartsText</h3>
<pre><code class="language-delphi">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;
</code></pre>
<h3 id="tstringhelperendswith和tstringhelperendstext">TStringHelper.EndsWith和TStringHelper.EndsText</h3>
<pre><code class="language-delphi">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;
</code></pre>
<h3 id="sametextansisametextsamestransisamestr">SameText、AnsiSameText、SameStr、AnsiSameStr</h3>
<pre><code class="language-delphi">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;
</code></pre>
<h3 id="comparetextansicomparetextcomparestransicomparestr">CompareText、AnsiCompareText、CompareStr、AnsiCompareStr</h3>
<pre><code class="language-delphi">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;
</code></pre>
<h3 id="strcomp和stricomp">StrComp和StrIComp</h3>
<pre><code class="language-delphi">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;
</code></pre>
<h3 id="tlocaleoptions">TLocaleOptions</h3>
<pre><code class="language-delphi">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;
</code></pre>
<p>返回结果</p>
<pre><code class="language-tex">不相等(Windows 平台)
不相等(Windows、OSX、iOS 和 Android 平台)
</code></pre><br><br>
来源:https://www.cnblogs.com/txgh/p/17497507.html
頁:
[1]