Pascal 字符串
Dancing with Strings
http://baskent.edu.tr/~tkaracay/etudio/ders/prg/pascal/PasHTM1/pas/pasl1007.html
Hi ! We meet again ! Now, I would like to discuss about strings in depth.
How are you doing ? Well, I'm having health problem this day and I need a
rest. I am still able to write this to you. Cheer !
Topics discussed this time are :
- String as array of char
- Limited length string
- String manipulation commands :
Length, Copy, Pos, Val, Str, Concat, Insert, Delete, Fillchar - String as a pointer of characters (PChar), introduction
Let's begin !
Actually, string is an array of characters. So, suppose s is a string. s[1]
is equal to the first character of s. s[2] is the second character, and so on. Look at this :
var
s : string;
begin
s:='Hello, dear';
writeln(s);
s[1]:='J'; { Replace the first character with J }
s[5]:='y'; { Replace the fifth character with y }
writeln(s); { Jelly, dear }
writeln('The length of s is ',ord(s[0]));
end.
Zeroth character is the character form of the length of that string. So, ord(s[0]) denotes the actual length of s. You may replace it with length(s) which gives the same effect.
Normally, strings would hold 80 characters in maximum. Suppose you would have a set of strings that is about 10 characters long. Declaring each as normal string would be a waste of space. Pascal provides facility to limit the string length. If you would like to have a string that has maximum limit of 10 characters, you would write :
var
s : string[10];
Pascal also provides routines to manipulate string :
- Length : return string length.
Syntax : length(s)
Example : n:=length(s);
Suppose s:='Who are you ?'; n would be 13. - Copy : get an excerpt from a string.
Syntax : copy(s,from,howmuch)
Example : st:=copy(s,5,3);
Get an excerpt of 3 characters from s, beginning from the 5th character.
Suppose s:='Who are you ?'; st will be 'are'.
Note that if index from is greater than the length of s, st would be
empty, example :
st:=copy(s,15,4); { empty string }
If howmuch exceed the end of the string s, it returns the remainder
of the string, example :
st:=copy(s,9,10); st would be equal to 'you ?' - Pos : get the position of a substring from a string.
Syntax : Pos(substr,s)
Example : n:=pos('are','Who are you ?'); { n:=5; }
If the substring is not found, it returns 0. - Val : converts strings to numeric.
Syntax : val(strvar,numvar,errorcode)
strvar is a string variable to be converted,
numvar is any numeric variable either integer or real, and
errorcode is an integer variable that holds the error code.
If errorcode is 0, conversion success. Otherwise, it points at the position
of strvar that cause failure. Example :
var
s : string;
e : integer;
r : real; begin
write('Enter a number : '); readln(s);
val(s,r,e);
if e<>0 then
writeln('Error at position : ',e);
else
writeln('That was : ',r:4:3);
end. - Str : converts numeric into strings.
Syntax : str(numvar,strvar)
Example :
var
s : string;
i : integer;
begin
write('Input an integer : '); readln(i);
str(i,s);
writeln('That was : ',s);
end.
Note that if you deal with real, you may format it before you convert it into strings. Suppose r is a real variable, s can be like this :str(r:4:3,s);
s consists of 4 digits before the decimal point of r, and 3 digits after the decimal point. Example :
var
s : string;
r : real;
begin
write('Input a real : '); readln(r);
str(r:4:3,s);
writeln('That was : ',s);
end. - Concat : Concatenates two or more strings
Syntax : concat(s1,s2,...,sn)
Example : st:=concat(s1,s2);
If s1='ABC' and s2='DEF', st would be 'ABCDEF'
st:=concat('Borland ','Pascal ','ver. ','7.0');
Would be 'Borland Pascal ver. 7.0'
You may put as many parameters to concat as possible. If the
resulting string length is more than 255, it will be truncated
to 255.
Concat is the same if we use plus sign (+). For example :
st:=concat('ABC','DEF'); is the same as
st:='ABC'+'DEF'; - Insert : Insert a string inside another string from indexth character
Syntax : insert(source,target,index)
Example :
var
s1, s2 : string;
begin
s1:='not ';
s2:='I do love you';
insert(s1,s2,6);
writeln(s2); { I do not love you }
end.
If the result string length is more than 255, it will be truncated into 255 as well. - Delete : Deletes n characters from string s starting from index i.
Syntax : delete(s,i,n);
If index i is greater than length of s, s is unchanged. If n specifies
more characters than remain (starting from i), the remainder of the
string is deleted. Example :
var
s : string;
begin
s:='I am not responsible for that !';
delete(s,6,3);
writeln(s); { I am responsible for that }
end. - Fillchar : fill string s with character c until s is n-1 char long.
Syntax : fillchar(s,n,c);
Beware : s[0] is overrided, so don't forget to add s[0]:=chr(n-1); to
normalize it.
var
s : string;
begin
fillchar(s,51,'=');
s[0]:=chr(50);
end.
Actually, those procedures or functions can be read from Pascal's help. So, refer to Borland Pascal help if you want working examples. You can even make your own string functions. If you don't understand, e-mail me.
As Borland Pascal 7.0 arrives, we know that C style string is adopted in. In C, we view strings either as an array of characters or a pointer of characters. Pointer will be discussed in second lesson. The main reason is that Pascal can not maintain strings larger than 255. Then a new type of string is employed : PChar, a pointer of character.
Pascal string consists of : one byte for its length then the contents. In PChar, just like C, we don't recognize the length. All we know is when the string stops -- that is when we encounter ASCII 0 (NULL). That null stop style makes the C style of string called NULL TERMINATED STRING.
All string manipulation routines for PChar are adopted from C. If you would like to know each one of it, you should learn C, where it came from. Nevertheless, you could even learn it in Borland Pascal help too.
The details of pointers and this type of string could be found in my second lesson of Pascal.
You could compare two strings just like numbers, too ! Suppose s1 and s2 are strings, you could do like this :
if s1 < s2 then .... (and so on ...)
That's all for this time.
Where to go ?
Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 7 about arrays
To Chapter 9 about records
My page of programming link
Contact me here
By : Roby Joehanes, © 1996, 2000
Pascal 字符串的更多相关文章
- 用结构体解析Pascal字符串
来源:https://www.cnblogs.com/qiuyuwutong/p/8708844.html 1.什么是柔性数组? 柔性数组既数组大小待定的数组, C语言中结构体的最后一个元素可以是大小 ...
- Inno Setup, Pascal 字符串带双引号如何写
Windows 的路径中如果有空格,就需要用双引号括起来.只能填 ASCII-Code-Number (decimal),不能用一般的 escape 方法. # + path + # 查询这个表的第一 ...
- Delphi字符串的基本操作与常用函数
参考:http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html 结合这个博客一起学习:http://www.cnblogs.c ...
- Delphi中的各种字符串、String、PChar、Char数组
参考博客:http://www.cnblogs.com/pchmonster/archive/2011/12/14/2287686.html 其中的所有代码均在Delphi7下测试通过. Delphi ...
- Delphi反汇编内部字符串处理函数/过程不完全列表
Delphi反汇编内部字符串处理函数/过程不完全列表 名称 参数 返回值 作用 等价形式 / 备注 _PStrCat EAX :目标字符串 EDX :源字符串 EAX 连接两个 Pascal 字符 ...
- Delphi反汇编内部字符串处理函数不完全列表
Delphi反汇编内部字符串处理函数/过程不完全列表 名称 参数 返回值 作用 等价形式 / 备注 _PStrCat EAX :目标字符串 EDX :源字符串 EAX 连接两个 Pascal 字符串 ...
- C++字符串之一(字符表示)
在C++中有两种类型可以用于表示字符,char和wchar_t. 但是字符串格式的标准却有很多种,如ASCII,UTF8,UTF16,UTF32等等.字符串的格式和char/wchar_t 的关系是什 ...
- delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
//from http://kingron.myetang.com/zsfunc0d.htm (*// 标题:充分利用pascal字符串类型 说明:和PChar不同,string可以保存# ...
- 关于Delphi中的字符串的浅析(瓢虫大作,里面有内存错误的举例)
关于Delphi中的字符串的浅析 只是浅浅的解析下,让大家可以快速的理解字符串. 其中的所有代码均在Delphi7下测试通过. Delphi 4,5,6,7中有字符串类型包括了: 短字符串(Short ...
随机推荐
- python基础学习-字符串常见操作
字符串常见操作 索引 s = "abcdefg" # 字符串数据,切片后取出的数据都是字符串类型 # 从左至右取值:从0开始 # 从右向左取值:从-1开始 print(" ...
- nginx 安装教程
Nginx 安装教程 本教程在CentOS6.7中安装nginx 1.8.0,Nginx中加入了taobao的concat模块,nginx-upload-module模块,pcre 以及nginx-u ...
- Lisp-02: 函数
函数(functions) 在 Lisp 中,函数分两种:有名函数和匿名函数(lambda函数). 有名函数 defun 有名函数的标准定义格式为: (defun <name> (list ...
- 通俗易懂.NET GC垃圾回收机制(适用于小白面试,大牛勿喷)
情景:你接到xx公司面试邀请,你怀着激动忐忑的心坐在对方公司会议室,想着等会的技术面试.技术总监此时走来,与你简单交谈后.... 技术:你对GC垃圾回收机制了解的怎么样? 你:还行,有简单了解过. 技 ...
- python基础-深浅拷贝
深拷贝与浅拷贝 总结: # 浅拷贝:list dict: 嵌套的可变数据类型是同一个 # 深拷贝:list dict: 嵌套的不可变数据类型彼此独立 浅拷贝 # 个人理解: # 在内存中重新创建一个空 ...
- Git应用详解第二讲:Git删除、修改、撤销操作
前言 前情提要:Git应用详解第一讲:Git分区,配置与日志 在第一讲中我们对Git进行了简单的入门介绍,相信聪明的你已经了解Git的基本使用了. 这一讲我们来进一步深入学习Git应用,着重介绍Git ...
- Java 给 PowerPoint 文档添加背景颜色和背景图片
在制作Powerpoint文档时,背景是非常重要的,统一的背景能让Powerpoint 演示文稿看起来更加干净美观.本文将详细讲述如何在Java应用程序中使用免费的Free Spire.Present ...
- Java第八天,抽象的概念是什么?如何完成抽象类的实现?
抽象 面向对象编程中,抽象是一个很重要的概念,那么抽象有什么需要注意的地方呢?请熟记以下知识点. 如果父类当中的方法不确定如何进行方法体的实现,则这个方法就是抽象方法. 抽象方法只需要在方法前面加上a ...
- GO代码风格指南 Uber Go (转载)
原文地址:https://github.com/uber-go/guide/blob/master/style.md 译文出处:https://github.com/uber-go/guide 本文永 ...
- tomcat查看线程数
获取tomcat进程pid ps -ef|grep tomcat 统计该tomcat进程内的线程个数 ps -Lf 29295 |wc -l