What are string operators in Pascal expressions?

Pascal strings – like strings in other programming languages – have several string manipulation methods. However, there is only one string operator, the + operator, that concatenates two strings or characters together.

Null-terminated strings declared as pchar cannot be concatenated and will raise an error.

Key points

  1. If the strings being concatenated are all short strings, the result is also a short string.

  2. If the strings being concatenated are all AnsiStrings, the result is also an AnsiString.

  3. If the two strings being concatenated include an AnsiString and a short string, the result is an AnsiString.

Code

Let’s look at the code to see how strings are concatenated in Pascal. We declare 5 strings, str, str1, str2, str3, str4, and perform concatenation operations between them using the + operator.

program Hello;
uses sysutils;
var
   str, str1, str2, str3, str4 : string;
   

begin
   str:= 'Hi ';
   str1 := 'Hello ';
   str2 := 'There!';
   
   str3 := str1 + str2;
   writeln(' str3 is = ', str3);
   str4:= str + str1;
   writeln(' str4 is = ', str4);
        
end.

Output

str3 is = Hello There!
str4 is = Hi Hello

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved