📄 010.htm
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->DELPHI基础教程-->第三章 字符串列表及应用(一)</TITLE>
<META NAME="keywords" CONTENT=" DELPHI基础教程 第三章 字符串列表及应用(一)">
<META NAME="description" CONTENT=" - DELPHI基础教程 - 第三章 字符串列表及应用(一)">
<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>
<TD CLASS="tt3" VALIGN="top" width="8%" bgcolor="#e0e0e0"><strong><A HREF="011.htm"><FONT style="FONT-SIZE: 9pt">后一页</font></A><BR>
<A HREF="009.htm"><FONT style="FONT-SIZE: 9pt">前一页</font></A><BR>
<A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目录</font></A><BR>
<A HREF="../../../../index.htm"><FONT style="FONT-SIZE: 9pt">回首页</font></A><BR>
</strong>
</TD>
<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">第三章 字符串列表及应用(一)</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<p> Delphi<font face="宋体" lang="ZH-CN">应用程序经常要处理字符串列表,如组合框和列表框中的字符串,</font>TMemo<font
face="宋体" lang="ZH-CN">部件的文本行,屏幕支持的字体列表,</font>TNotebook<font
face="宋体" lang="ZH-CN">部件的</font>tabs<font face="宋体" lang="ZH-CN">属性,字符串网格的行、列等等。</p>
<p> 虽然应用程序以不同的方法使用这些列表,但</font>Delphi<font
face="宋体" lang="ZH-CN">通过一个叫字符串列表</font>(Tstrings)<font
face="宋体" lang="ZH-CN">的对象提供统一的界面,并且在不同场合可相互转化。例如,可以在</font>TMemo<font
face="宋体" lang="ZH-CN">部件中编辑某一字符串,并把它当成列表框中列表项使用。</p>
<p> 在</font>Delphi<font face="宋体" lang="ZH-CN">集成开发环境中也经常要使用字符串列表。如在</font>Object
Inspector<font face="宋体" lang="ZH-CN">窗体的取值栏中常列有</font>Tstrings<font
face="宋体" lang="ZH-CN">字符,双击该字符,将弹出字符列表编辑器,如图</font>3.1<font
face="宋体" lang="ZH-CN">,在编辑器中可进行编辑、加入、删除等操作</font>
<font face="宋体" lang="ZH-CN">。</p>
<p> 在运行状态时也可以操作字符串列表,常见的字符串列表操作如下:</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">列表中操作字符串</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">装载、保存字符串列表</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">创建字符串列表</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">在字符串列表中加入对象</font></p>
<p><font face="宋体" lang="ZH-CN">本章将介绍字符串列表的常用操作及简单应用。</font>
</p>
<p>3.1 <font face="宋体" lang="ZH-CN">字符串列表的常用操作 </p>
</font><p>3.1.1 <font face="宋体" lang="ZH-CN">列表中操作字符串 </p>
<p> 在</font>Delphi<font face="宋体" lang="ZH-CN">应用程序中,经常要对列表中的字符串进行操作。例如,设计时修改字符串列表属性。</p>
<p> 常见的字符串操作如下:</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">计算列表中字符串数目</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">访问指定字符串</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">查找字符串的位置</font></p>
<p><font face="宋体" lang="ZH-CN">●</font> <font face="宋体" lang="ZH-CN">往列表中加入字符串</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">删除列表中的字符串</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">在列表中移动字符串</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">复制一个完整的字符串列表</p>
<p> ●</font> <font face="宋体" lang="ZH-CN">复制列表中的字符串 </p>
</font><p>3.1.1.1 <font face="宋体" lang="ZH-CN">计算列表中的字符串数目 </p>
<p> 使用</font>Count<font face="宋体" lang="ZH-CN">属性可计算列表中的字符串数目。</font>Count<font
face="宋体" lang="ZH-CN">是只读属性,用以指示列表中字符串列表数目。因为字符串列表是以零开始索引,因而</font>Count<font
face="宋体" lang="ZH-CN">比列表的最大索引数大一。</p>
<p> 例如,应用程序想计算当前屏幕支持的字体数目,可查找屏幕对象的字体列表,该列表包含了屏幕支持的所有字体的名字。</p>
<p> </font>FontCount:=Screen.Fonts.Count;</p>
<p>3.1.1.2 <font face="宋体" lang="ZH-CN">访问指定字符串 </p>
<p> 字符串列表有一个可索引的</font>Strings<font face="宋体"
lang="ZH-CN">属性,可象使用字符串数组一样使用</font>Strings<font
face="宋体" lang="ZH-CN">。例如,列表中第一个字符串为</font>Strings[0]<font
face="宋体" lang="ZH-CN">。因为</font>Strings<font face="宋体" lang="ZH-CN">属性为字符串列表中最常用的属性,</font>Strings<font
face="宋体" lang="ZH-CN">属性可做为字符串列表的缺省属性,即使用时可省略</font>Strings<font
face="宋体" lang="ZH-CN">标识符。</p>
<p> 要访问字符串中的指定字符,可查找该字符的起始位置或索引。字符串数目是以零开始记数的。如果列表中有三个字符串,其索引范围为</font>0..2<font
face="宋体" lang="ZH-CN">。</p>
<p> 以下代码是等价的: </p>
<p> </font>Memol.Lines.Strings[0]:='This is the first line.';</p>
<p>
Memol.Lines[0]:='This
is the first line.';<font face="宋体" lang="ZH-CN"> </font></p>
<p>3.1.1.3 <font face="宋体" lang="ZH-CN">查找字符串的位置 </p>
<p> </font>Indexof<font face="宋体" lang="ZH-CN">方法可查找指定字符串的位置。</font>Indexof<font
face="宋体" lang="ZH-CN">有一个字符串类型的参数,方法返回列表中匹配字符串的位置。如果列表中无匹配字符串,将返回</font>-
1<font face="宋体" lang="ZH-CN">。</font></p>
<p> Indexof<font
face="宋体" lang="ZH-CN">方法只能查找完整字符串,即必须完全匹配整个字符串。如果只匹配部分字符串,必须编写相应代码。</p>
<p> 以下代码判定列表中是否有指定字符串:</font></p>
<p>if FileListBox1.Items.IndexOf('AUTOEXEC.BAT') > -1 then</p>
<p>begin</p>
<p>Color := clYellow;</p>
<p>Label1.Caption := 'You are in the root directory!';</p>
<p>end; </p>
<p>3.1.1.4 <font face="宋体" lang="ZH-CN">在列表中加入字符串 </p>
<p> 有两种方式往列表中加入字符串:可把字符串加到列表的最后,也可插入列表之中。</p>
<p> 要把字符串加至列表尾部,使用</font>Add<font face="宋体"
lang="ZH-CN">方法,把字符串作为参数传递。</p>
<p> 要把字符串插入列表中,使用</font>Insert<font face="宋体"
lang="ZH-CN">方法,传递两个参数:插入的位置和字符串。</p>
<p> 例如,要把“</font>Three<font face="宋体" lang="ZH-CN">”插入至列表中的第三个位置,使用代码</font>Insert(2,'Three')<font
face="宋体" lang="ZH-CN">。如果列表中的字符不到</font>2<font face="宋体"
lang="ZH-CN">个,</font>Delphi<font face="宋体" lang="ZH-CN">将产生超出索引范围的异常</font>(<font
face="宋体" lang="ZH-CN">关于异常详见十二章</font>)<font face="宋体"
lang="ZH-CN">。 </p>
</font><p>3.1.1.5 <font face="宋体" lang="ZH-CN">在列表中移动字符串 </p>
<p> 应用程序可以在列表中把指定字符串移至另一个位置,如果字符串与某个对象相连,则该对象与字符串同步移动。</p>
<p> </font>Move<font face="宋体" lang="ZH-CN">方法可实现字符串的移动,它有两个参数:现行位置和要移动的位置。以下代码把第三个字符串移至第五的位置: </p>
<p> </font>Move(2,4); </p>
<p>3.1.1.6 <font face="宋体" lang="ZH-CN">删除列表中的字符串 </p>
<p> 使用</font>Delete<font face="宋体" lang="ZH-CN">方法可以删除指定的字符串。</font>Delete<font
face="宋体" lang="ZH-CN">的参数是指定字符串的位置,如果不知道字符串的位置,可使用</font>Indexof<font
face="宋体" lang="ZH-CN">方法。</p>
<p> 要删除字符串列表中所有的字符串,可使用</font>Clear<font
face="宋体" lang="ZH-CN">方法。</p>
<p> 以下代码删除列表框中的指定字符串: </p>
<p> </font>With ListBox1.Items do</p>
<p>begin</p>
<p>if Indexof('bureaucracy')>-1 then</p>
<p>Delete (Indexof('bureaucracy'));</p>
<p>end; </p>
<p>3.1.1.7 <font face="宋体" lang="ZH-CN">复制完整的字符串列表 </p>
<p> 把一个列表复制到另一个列表相当于把源列表赋值给目标列表,即使列表从属于不同的部件,</font>Delphi<font
face="宋体" lang="ZH-CN">也可以进行这种复制。</p>
<p> 复制列表将覆盖掉目标列表,如果要把源列表加到目标列表的尾部,使用</font>Addstrings<font
face="宋体" lang="ZH-CN">方法。</p>
<p> 以下代码分别为复制列表和连接列表: </p>
</font><p>Outline1.Lines:=ComboBox1.Items;</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -