String is a built-in data type in Pascal used to store a sequence of characters. The declaration of
String in Pascal is shown below:
_variableName_ : String = _stringBody_;
_variableName_: The variable name that identifies the variable.
_stringBody_: The body of the string which is a sequence of characters. The body of the string is enclosed in single quotes.
The length of a string is the number of characters stored in it. The string length in Pascal is calculated as shown below:
length := byte(str);
The code snippet above stores the string length of
To access a character of a string, use the
 operator, as shown below:
character = str[ind];
The code snippet above stores the character at the index
Consider the code snippet below, which demonstrates the declaration of
program stringExample1; var str: String = 'Hello world'; begin writeln(str); end.
Consider the code snippet below, which calculates the length of a string and prints its first and last character:
program stringExample2; var str: String = 'Hello world'; begin writeln('String length = ', byte(str)); writeln('First character in str = ', str); writeln('Last character in str = ', str[byte(str)]); end.
byte(str) is the number of characters in
str[ind] is used to print the character of
str at index
str will print the first index of
str[byte(str)] prints the character at the last index, as
byte(str) is equal to the length of
View all Courses