我在东莞混日子 發表於 2021-1-7 10:16:00

Delphi 字符串拆分/分割[1] - TStringList

<p><span style="font-size: 16px"><strong>Delphi 字符串拆分/分割 - TStringList&nbsp;<br></strong></span></p>
<p><span style="font-size: 16px">1、TStringList 默认以 ','拆分字符</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">onst
constr :String = 'aaa,bbb,ccc,ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.CommaText := constr;
for i := 0 to Strs.Count-1 do
    ShowMessage(Strs);   //aaa&nbsp;bbb&nbsp;ccc&nbsp;ddd
end;
</span></pre>
</div>
<p><span style="font-size: 16px">2、通过&nbsp;Delimiter 定义分割字符</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">const
constr :String = 'aaa\bbb\ccc\ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
    ShowMessage(Strs);   
end;
</span></pre>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">const
constr :String = '"aaa"\"bbb"\"ccc"\"ddd"';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
    ShowMessage(Strs);
end;
</span></pre>
</div>
<p><span style="font-size: 16px">3、增加 QuoteChar 属性 分割   </span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">const
constr :String = '|aaa|\|bbb|\|ccc|\|ddd|';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.Delimiter := '\';
strs.QuoteChar := '|';//QuoteChar。其默认值为:'"'(不包括单引号)
strs.DelimitedText := constr;
for i := 0 to Strs.Count-1 do
    ShowMessage(Strs);
end;
</span></pre>
</div>
<p><span style="font-size: 16px">4、通过&nbsp;Names &amp; Values &amp; ValueFromIndex&nbsp;</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">const
constr :String = '0=aaa,1=bbb,2=ccc,3=ddd';
var
strs :TStrings;
i :Integer;
begin
strs := TStringList.Create;
strs.CommaText := constr;
for i := 0 to strs.Count-1 do
begin
    ShowMessage(strs.Names);
    ShowMessage(strs.Values]);
    ShowMessage(strs.ValueFromIndex);
end;
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span> </p>
<p>  </p>
<p><span style="color: rgba(136, 136, 136, 1)">创建时间:2021.01.07  更新时间:</span></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/14244947.html
頁: [1]
查看完整版本: Delphi 字符串拆分/分割[1] - TStringList