秋江望月 發表於 2023-9-12 14:44:00

delphi截取字符串心得

<p>在Delphi的日常开发中,确实经常需要截取字符串来满足不同的需求。Delphi自身提供了一些字符串处理函数,但有时候这些函数可能不够用或者使用起来不够方便。下面我将分享一个自己在Delphi开发中用于截取字符串的进一步封装的函数。</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">////////////////////////////////////</span><span style="color: rgba(0, 128, 0, 1)">/</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提取字符串中指定子字符串前的字符串</span>
function Before(Src, S: <span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
F: Integer;
begin
F :</span>=<span style="color: rgba(0, 0, 0, 1)"> Pos(Src, S);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> F = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> then
    Before :</span>=<span style="color: rgba(0, 0, 0, 1)"> S
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    Before :</span>= Copy(S, <span style="color: rgba(128, 0, 128, 1)">1</span>, F - <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">);
end;
</span><span style="color: rgba(128, 128, 128, 1)">/////////////////////////////////</span><span style="color: rgba(0, 128, 0, 1)">/</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提取字符串中指定子字符串后的字符串</span>
<span style="color: rgba(0, 0, 0, 1)">
function After(Src, S: </span><span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
F: Integer;
begin
F :</span>=<span style="color: rgba(0, 0, 0, 1)"> Pos(Src, S);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> F = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> then
    After :</span>= <span style="color: rgba(128, 0, 0, 1)">''</span>
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    After :</span>= Copy(S, F +<span style="color: rgba(0, 0, 0, 1)"> Length(Src), Length(S));
end;

function ReversePos(SubStr, S: </span><span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): Integer;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
i: Integer;
begin
i :</span>=<span style="color: rgba(0, 0, 0, 1)"> Pos(ReverseString(SubStr), ReverseString(S));
</span><span style="color: rgba(0, 0, 255, 1)">if</span> i &gt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> then
    i :</span>= Length(S) - i - Length(SubStr) + <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">;
Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> i;
end;
// 从头取到最后包含的代码
function afterlast(SubStr, S: </span><span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)">
i: Integer;
begin
i :</span>=<span style="color: rgba(0, 0, 0, 1)"> Pos(ReverseString(SubStr), ReverseString(S));
</span><span style="color: rgba(0, 0, 255, 1)">if</span> i &gt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> then
begin
    i :</span>= Length(S) - i - Length(SubStr) + <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Result := i;</span>
<span style="color: rgba(0, 0, 0, 1)">
    Result :</span>= Copy(S, i +<span style="color: rgba(0, 0, 0, 1)"> Length(SubStr), Length(S));
end
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    Result :</span>=<span style="color: rgba(0, 0, 0, 1)"> S;

end;</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这是一个函数定义,名为Before,功能是提取在指定子字符串之前的字符串</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">Src是源字符串,S是需要寻找的子字符串</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> Before(Src, S: <span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义一个整型变量F,用于存储子字符串在源字符串中的位置</span>
<span style="color: rgba(0, 0, 0, 1)">F: Integer;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用Pos函数查找子字符串S在源字符串Src中的位置,并将结果赋值给F</span>
F :=<span style="color: rgba(0, 0, 0, 1)"> Pos(Src, S);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果F等于0,说明源字符串中没有找到子字符串,那么返回的结果就是子字符串S本身</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> F = <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
    Before :</span>=<span style="color: rgba(0, 0, 0, 1)"> S
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果F不等于0,说明源字符串中找到了子字符串,那么就返回从字符串开始到子字符串位置F-1的这部分字符串</span>
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    Before :</span>= Copy(S, <span style="color: rgba(128, 0, 128, 1)">1</span>, F - <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这是一个函数定义,名为After,功能是提取在指定子字符串之后的字符串</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">Src是源字符串,S是需要寻找的子字符串</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> After(Src, S: <span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义一个整型变量F,用于存储子字符串在源字符串中的位置</span>
<span style="color: rgba(0, 0, 0, 1)">F: Integer;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用Pos函数查找子字符串S在源字符串Src中的位置,并将结果赋值给F</span>
F :=<span style="color: rgba(0, 0, 0, 1)"> Pos(Src, S);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果F等于0,说明源字符串中没有找到子字符串,那么返回的结果就是空字符串</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> F = <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
    After :</span>= <span style="color: rgba(128, 0, 0, 1)">''</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果F不等于0,说明源字符串中找到了子字符串,那么就返回从子字符串位置F之后的这部分字符串</span>
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
    After :</span>= Copy(S, F +<span style="color: rgba(0, 0, 0, 1)"> Length(Src), Length(S));
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义一个函数名为ReversePos,功能是找到子字符串在源字符串中的最后一个出现位置的索引</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">SubStr是目标子字符串,S是源字符串</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> ReversePos(SubStr, S: <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">): Integer;
</span><span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义一个整型变量i,用于存储子字符串在源字符串中的位置</span>
<span style="color: rgba(0, 0, 0, 1)">i: Integer;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用Pos函数查找反转后的子字符串在反转后的源字符串中的位置,并将结果赋值给i</span>
i :=<span style="color: rgba(0, 0, 0, 1)"> Pos(ReverseString(SubStr), ReverseString(S));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果i大于0,说明在反转后的源字符串中找到了反转后的子字符串,那么将i转换成正数,否则保持为0</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> i &gt; <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
    i :</span>= Length(S) - i - Length(SubStr) + <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将结果返回</span>
Result :=<span style="color: rgba(0, 0, 0, 1)"> i;
</span><span style="color: rgba(0, 0, 255, 1)">end</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义一个函数名为afterlast,功能是提取在指定子字符串最后一次出现之后的字符串</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">SubStr是目标子字符串,S是源字符串</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> afterlast(SubStr, S: <span style="color: rgba(0, 0, 255, 1)">string</span>): <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义一个整型变量i,用于存储子字符串在源字符串中的位置</span>
<span style="color: rgba(0, 0, 0, 1)">i: Integer;
</span><span style="color: rgba(0, 0, 255, 1)">begin</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用Pos函数查找反转后的子字符串在反转后的源字符串中的位置,并将结果赋值给i</span>
i :=<span style="color: rgba(0, 0, 0, 1)"> Pos(ReverseString(SubStr), ReverseString(S));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果i大于0,说明在反转后的源字符串中找到了反转后的子字符串</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> i &gt; <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(0, 0, 255, 1)">then</span>
<span style="color: rgba(0, 0, 255, 1)">begin</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">计算出子字符串最后一次出现的位置的索引,并将长度为SubStr的子串从源字符串的该位置开始截取出来赋值给Result变量</span>
    i := Length(S) - i - Length(SubStr) + <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">;
    Result :</span>= Copy(S, i + Length(SubStr), Length(S));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将结果返回为从最后一次出现位置后面到源字符串结束的部分。这里应该是Result := Copy(S, i + Length(SubStr), Length(S));这一段注释才是正确的。之前的Result := i;这一行注释是不正确的。因为我们需要的是子串之后的部分,而不是位置索引。感谢你的纠正。 修改完毕。 】这段注释有误。应该是将结果返回为从最后一次出现位置后面到源字符串结束的部分。所以应该是Result := Copy(S, i + Length(SubStr), Length(S));这一行。之前的Result := i;这一行注释是不正确的。因为我们需要的是子串之后的部分,而不是位置索引。这段注释已经修改。感谢你的指正。 修改完毕。 】Result := Copy(S, i + Length(SubStr), Length(S));// 将结果返回为从最后一次出现位置后面到源字符串结束的部分。这一行注释是对的<br>end else //如果i等于0,说明源字符串中没有找到子字符串,那么返回的结果就是源字符串本身<br> Result := S; <br>end;<br></span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/wskc/p/17696162.html
頁: [1]
查看完整版本: delphi截取字符串心得