📄 pasl1007.html
字号:
<HTML>
<head>
<LINK href="../style.css" rel="stylesheet" type="text/css">
<TITLE>Pascal Tutorial - Chapter 8</Title>
</head>
<body background="../tile01.jpg">
<H1><center>Dancing with Strings</center></h1><p> <br><br>
<p>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 :<ol>
<li>String as array of char</li>
<li>Limited length string</li>
<li>String manipulation commands :<br>
<tt>Length, Copy, Pos, Val, Str, Concat, Insert, Delete, Fillchar</tt></li>
<li>String as a pointer of characters (<tt>PChar</tt>), introduction</li></ol>
Let's begin !<p>
Actually, <tt>string</tt> is an <tt>array</tt> of characters. So, suppose s is a string. <tt>s[1]</tt>
is equal to the first character of s. <tt>s[2]</tt> is the second character, and so on. Look at this :</p><hr><pre>
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.
</pre><hr><p>
Zero<sup>th</sup> character is the character form of the length of that string. So,
<tt>ord(s[0])</tt> denotes the actual length of s. You may replace it with <tt>length(s)</tt>
which gives the same effect.</p>
<p>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 :</p><pre>
var
s : string[10];
</pre><p>Pascal also provides routines to manipulate string :<ol>
<li><b>Length</b> : return string length.<br>
Syntax : <tt>length(s)</tt><br>
Example : <tt>n:=length(s);</tt><br>
Suppose <tt>s:='Who are you ?';</tt> n would be 13.</li>
<li><b>Copy</b> : get an excerpt from a string.<br>
Syntax : <tt>copy(s,from,howmuch)</tt><br>
Example : <tt>st:=copy(s,5,3);</tt><br>
Get an excerpt of 3 characters from s, beginning from the 5<sup>th</sup> character.<br>
Suppose <tt>s:='Who are you ?';</tt> st will be 'are'.<br>
Note that if index <tt>from</tt> is greater than the length of s, st would be
empty, example :<br><tt>st:=copy(s,15,4); { empty string }</tt><br>
If <tt>howmuch</tt> exceed the end of the string s, it returns the remainder
of the string, example :<br><tt>st:=copy(s,9,10);</tt> st would be equal to 'you ?'</li>
<li><b>Pos</b> : get the position of a substring from a string.<br>
Syntax : <tt>Pos(substr,s)</tt><br>
Example : <tt>n:=pos('are','Who are you ?'); { n:=5; }</tt><br>
If the substring is not found, it returns 0.</li>
<li><b>Val</b> : converts strings to numeric.<br>
Syntax : <tt>val(strvar,numvar,errorcode)</tt><br>
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 :<hr><pre>
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.
</pre><hr></li>
<li><b>Str</b> : converts numeric into strings.<br>
Syntax : <tt>str(numvar,strvar)</tt><br>
Example :<hr><pre>
var
s : string;
i : integer;
begin
write('Input an integer : '); readln(i);
str(i,s);
writeln('That was : ',s);
end.
</pre><hr>
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 :<br><pre>
str(r:4:3,s);
</pre>s consists of 4 digits before the decimal point of r, and 3 digits after
the decimal point. Example :<hr><pre>
var
s : string;
r : real;
begin
write('Input a real : '); readln(r);
str(r:4:3,s);
writeln('That was : ',s);
end.
</pre><hr></li>
<li><b>Concat</b> : Concatenates two or more strings<br>
Syntax : <tt>concat(s1,s2,...,sn)</tt><br>
Example : <tt>st:=concat(s1,s2);</tt><br>
If <tt>s1='ABC'</tt> and <tt>s2='DEF'</tt>, st would be <tt>'ABCDEF'</tt><br>
<tt>st:=concat('Borland ','Pascal ','ver. ','7.0');</tt>
Would be <tt>'Borland Pascal ver. 7.0'</tt><br>
You may put as many parameters to <tt>concat</tt> as possible. If the
resulting string length is more than 255, it will be truncated
to 255.<br>
Concat is the same if we use plus sign (+). For example :<br>
<tt>st:=concat('ABC','DEF');</tt> is the same as
<tt>st:='ABC'+'DEF';</tt></li>
<li><b>Insert</b> : Insert a string inside another string from indexth character<br>
Syntax : <tt>insert(source,target,index)</tt><br>
Example :<hr><pre>
var
s1, s2 : string;
begin
s1:='not ';
s2:='I do love you';
insert(s1,s2,6);
writeln(s2); { I do not love you }
end.
</pre><hr>
If the result string length is more than 255, it will be truncated into
255 as well.</li>
<li><b>Delete</b> : Deletes n characters from string s starting from index i.<br>
Syntax : <tt>delete(s,i,n);</tt><br>
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 :<hr><pre>
var
s : string;
begin
s:='I am not responsible for that !';
delete(s,6,3);
writeln(s); { I am responsible for that }
end.
</pre><hr></li>
<li><b>Fillchar</b> : fill string s with character c until s is n-1 char long.<br>
Syntax : <tt>fillchar(s,n,c);</tt><br>
Beware : <tt>s[0]</tt> is overrided, so don't forget to add <tt>s[0]:=chr(n-1);</tt> to
normalize it.<hr><pre>
var
s : string;
begin
fillchar(s,51,'=');
s[0]:=chr(50);
end.
</pre><hr></ol>
<p>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
<a href="../faq.html">me</a>.</p>
<p>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 : <tt>PChar</tt>, a pointer of character.</p>
<p>Pascal string consists of : one byte for its length then the contents. In
<tt>PChar</tt>, 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 <b>NULL TERMINATED STRING.</b></p>
<p>All string manipulation routines for <tt>PChar</tt> 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.</p>
<p>The details of pointers and this type of string could be found in my second
lesson of Pascal.</p><hr>
<p>You could compare two strings just like numbers, too ! Suppose s1 and s2
are strings, you could do like this :</p><pre>
if s1 < s2 then .... (and so on ...)
</pre>
<p>That's all for this time.</p>
<hr><p><B><H3>Where to go ?</H3></B><p>
<A HREF="../news.html">Back to main page</A><BR>
<A HREF="pasles01.html">Back to Pascal Tutorial Lesson 1 contents</A><BR>
<A HREF="pasq1007.html">To the quiz</A><BR>
<A HREF="pasl1006.html">Back to Chapter 7</A> about arrays<BR>
<A HREF="pasl1008.html">To Chapter 9</A> about records<BR>
<A HREF="../mylink.html">My page of programming link</A><BR>
<A HREF="../faq.html">Contact me here</A>
<hr><P CLASS="cpy">By : Roby Joehanes, © 1996, 2000</P>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -