⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formulas_5__string_functions.html

📁 语音信号处理软件praat教程:这是一部入门教程
💻 HTML
字号:
<html><head><meta name="robots" content="index,follow">
<title>算式 5. 字符串函数</title></head><body bgcolor="#FFFFFF">

<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b>
算式 5. 字符串函数
</b></font></table></table>
<p>
字符串函数指函数返回值为字符串或参数中包含至少一个字符串的函数。由于字符串计算在<a href="Calculator.html">计算器</a>、设置窗口或用来创建与修改对象的算式中并无大用,本文仅提供在脚本中使用字符串的范例,所以这些例子将包含字符串变量。</p>
<dl>
<dt>
<b>length (a$)</b>
<dd>
返回字符串长度。比如运行
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
string$ = "hallo"<br></code>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
length = length (string$ + "dag")<br></code>
<dl>
<dd>
之后,变量<i>length</i>即存储8这个数。这个例子还表明:变量可以和函数同名,不用担心解释程序会有什么混淆。
<dt>
<b>left$ (a$, n)</b>
<dd>
返回由<i>a$</i>的前<i>n</i>个字符组成的字符串。比如运行
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
head$ = left$ ("hallo", 3)<br></code>
<dl>
<dd>
之后,变量<i>head$</i>即存储"hal"这个字符串。
<dt>
<b>right$ (a$, n)</b>
<dd>
返回由<i>a$</i>的后<i>n</i>个字符组成的字符串。比如运行
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
english$ = "he" + right$ ("hallo", 3)<br></code>
<dl>
<dd>
之后,变量<i>english$</i>即存储"hello"这个字符串。
<dt>
<b>mid$ ("hello" , 3, 2)</b>
<dd>
返回由源字符串"hello"从第3个字符起的2个字符组成的字符串。结果:ll。
<dt>
<b>index (a$, b$)</b>
<dd>
返回字符串<i>b$</i>在字符串<i>a$</i>中第一次出现的位置索引。比如运行
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
where = index ("hallo allemaal", "al")<br></code>
<dl>
<dd>
之后,变量<i>where</i>即存储2这个数,因为第一个"al"正好始于较长字符串第2个字符的位置。假如第1个字符串参数并不包含第2个字符串参数,<i>index</i>返回0值。
<dt>
<b>rindex (a$, b$)</b>
<dd>
返回字符串<i>b$</i>在字符串<i>a$</i>中最后一次出现的位置索引。比如运行
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
where = rindex ("hallo allemaal", "al")<br></code>
<dl>
<dd>
之后,变量<i>where</i>即包含13这个数,因为最后一个"al"正好始于第13个字符的位置。假如第1个字符串参数并不包含第2个字符串参数,<i>index</i>返回0值。
<dt>
<b>fixed$ (number, precision)</b>
<dd>
按字符串返回一个格式化的数值,保留<i>precision</i>位小数。也就是说,<code>fixed$ (72.65687, 3)</code>返回字符串<code>72.657</code>,而<code>fixed$ (72.65001, 3)</code>返回字符串<code>72.650</code>。这两个例子表明:结果按四舍五入计算,末尾的0作为有效数字保留。但函数总是至少保留一位非0有效数字,比如说,<code>fixed$ (0.0000157, 3)</code>返回字符串<code>0.00002</code>。数值0一概返回字符串<code>0</code>。
<dt>
<b>percent$ (number, precision)</b>
<dd>
类似<b>fixed$</b>函数,但会加上百分号。举例:<code>percent$(0.157, 3)</code>返回字符串<code>15.700%</code>,<code>percent$(0.000157, 3)</code>返回字符串<code>0.016%</code>,而<code>percent$(0.000000157, 3)</code>返回字符串<code>0.00002%</code>。数值0一概返回字符串<code>0</code>。
<dt>
<b>date$ ()</b>
<dd>
按如下格式返回日期和时刻:
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Mon Jun 24 17:11:21 2002<br></code>
<dl>
<dd>
想要向Info(情报)窗口输出当月的日数,只要这样写:
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
date$ = date$ ()<br></code>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
day$ = mid$ (date$, 9, 2)<br></code>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
echo The month day is 'day$'.<br></code>
<dl>
<dt>
<b>extractNumber ("Type: Sound'newline$'Name: hello there'newline$'Size: 44007", "Size:")</b>
<dd>
在长字符串中,从"Size:"第一次出现的位置往后查,找出一个数值。结果:44007。这对于需要从长篇报表中抽取情报特别有用,比如下面的脚本,在声音编辑器窗口中运行:
</dl>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
report$ = Settings report<br></code>
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
maximumFrequency = extractNumber (report$, "Spectrogram maximum frequency:")<br></code>
<dl>
<dt>
<b>extractWord$ ("Type: Sound'newline$'Name: hello there'newline$'Size: 44007", "Type:")</b>
<dd>
在长字符串中,从"Type:"第一次出现的位置往后查,找出一个不含空格的单词。结果:Sound。
<dt>
<b>extractLine$ ("Type: Sound'newline$'Name: hello there'newline$'Size: 44007", "Name: ")</b>
<dd>
在长字符串中,从"Name: "第一次出现的位置往后,截取当行剩余文本(含空格)返回。结果:hello there。注意:特别的,"Name: "字符串含有一个空格,故而“当行剩余文本”起自<i>h</i>处。
</dl>
<h3>指向本页的链接</h3>
<ul>
<li><a href="Formulas.html">算式</a>
<li><a href="What_s_new_.html">最近更新</a>
</ul>
<hr>
<address>
	<p>&copy; ppgb, April 14, 2004</p>
	<p>&copy; 翻译:徐清白,2005年02月11日</p>
</address>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -