<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[文本子串运算]]></title><description><![CDATA[<p dir="auto"> </p>
<h2><a class="anchor-offset" name="先修科目"></a>先修科目</h2>
<ul>
<li><a href="/topic/527/%E6%96%87%E6%9C%AC%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E8%BF%90%E7%AE%97?_=1663119219949">文本中的字符运算</a></li>
</ul>
<h2><a class="anchor-offset" name="介绍"></a>介绍</h2>
<p dir="auto">大家已经学过<a href="/topic/527/%E6%96%87%E6%9C%AC%E4%B8%AD%E7%9A%84%E5%AD%97%E7%AC%A6%E8%BF%90%E7%AE%97?_=1663119219949">文本中的字符运算</a>。大家还可以做文本中“子串”运算。“子串”就是文本的一个片段。“子串”包含若干个连续的字符。</p>
<p dir="auto">例如文本“心有灵犀一点通”，你可以取位置3、4的两个字符组成一个子串“灵犀”：</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2e006a4d-d66c-494c-a113-b56d6366b151.png" width="400" /></p>
<p dir="auto"> </p>
<p dir="auto">注意：你不能取字符“灵”和“通”组成子串“灵通”。尽管这两个字都在文本中，但不相连，中间还隔着三个字。</p>
<p dir="auto"> <br />
下面看看处理文本子串的一些运算积木。</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="文本是否包含某个子串"></a>文本是否包含某个子串</h2>
<p dir="auto">一个最常用情形就是检查文本是否包含某个子串。例如文本“自古英雄出少年”，“英雄”是一个子串，而“英出”就不是。“true”表示“真”，“false”表示“假”，以下不再赘述。<br />
注意，英文字母不区分大小写，“BC”与“bc”相同。</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4a57e00e-8f15-4bf1-9bbe-b25f3fd407be.gif" width="360" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="文本中子串的位置"></a>文本中子串的位置</h2>
<p dir="auto">如果某个子串在文本中，你就可以获取子串在文本中的起始位置。例如，在文本“心有灵犀一点通”，子串“灵犀”位于3至4位置，那么其起始位置就是3。</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5c03b767-cd6c-4e7d-a6c8-f0613ff2b42e.gif" width="360" /></p>
<p dir="auto">如果文本中没有找到该子串，就返回值0。注意，英文字母不区分大小写。</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3a599a00-62f9-4a77-9696-8dcbf54aa90b.gif" width="400" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="提取文本的子串"></a>提取文本的子串</h2>
<p dir="auto">如果你知道自己要找的子串的起始位置和结束位置，就可以用积木“提取( )的子串”来提取子串。如果位置无效，就返回一个空值。</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/54d5cb04-9edf-45b0-9d23-0d6839d35cd4.gif" width="500" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="替换文本中的子串"></a>替换文本中的子串</h2>
<p dir="auto">你可以将文本中的子串替换为另一个字符串。例如文本“零丁洋里叹零丁”，子串“零丁”出现了2次。我们可以替换为另一个字符串，比如“大海”：</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/938b5b3a-5296-43fe-82ec-d11679525cf4.gif" width="550" /></p>
<p dir="auto">3个输入框依次为要修改的文本、被替换的子串、替换成的新字符串。</p>
]]></description><link>https://forum.creaticode.com/topic/608/文本子串运算</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 05:24:43 GMT</lastBuildDate><atom:link href="https://forum.creaticode.com/topic/608.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 14 Sep 2022 08:25:38 GMT</pubDate><ttl>60</ttl></channel></rss>