Pascal. Character strings of type String. Functions for working with character strings

Character strings of type String. Functions for working with character strings. Functions Concat, Copy, Delete, Insert, Length, Pos, Val, Str


Contents


Search other resources:

1. The Concat function. Concatenation (merging) of strings

To concatenate (add, merge) strings, use the Concat function. The general form of using the function is as follows

ResStr := Concat(S1:string; S2:string [; ... SN:string])

here

  • S1, S2, SN – the strings to be concatenated;
  • ResStr – resulting string.

To call a function, you must specify at least one parameter string. The result of the function execution is the resulting string of the string type.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S1, S2, S3, ResStr : String;

begin

  // Function Concat() - string concatenation

  S1 := 'Hello';
  S2 := ',';
  S3 := ' world';
  ResStr := Concat(S1, S2, S3, '!');

  Writeln(ResStr); // Hello, world!

  Readln;
end.

Program result

Hello, world!

 

2. Function Copy. Get a copy of a substring from a specified string

To get some fragment from a string, the Copy function is used, which has the following usage

ResStr := Copy(S, Start, Len);

here

  • S – string value of the String type, from which the fragment is selected;
  • Start is an Integer type value that determines the position of the beginning of the selected fragment;
  • Len is an Integer type value that determines the number of fragment characters to select.
  • ResStr – the result of this procedure is a value of type String.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S1, S2, S3: String;

begin

  // Function Copy() - get a substring from a string
  S1 := '1234567890'; // original string

  S2 := Copy(S1, 4, 3); // S2 = '456'
  Writeln(S2);

  S3 := Copy(S1, 8, 5); // S3 = '890'
  Writeln(S3);

  Readln;
end.

Program result

456
890

 

3. Procedure Delete. Removing a substring from a string

Using the Delete procedure, you can remove some fragment from a string. General view of the procedure:

Delete(S, Start, Len)

here

  • S – string value from which the fragment is removed;
  • Start – sequence number of the first character of the fragment to be deleted;
  • Len – the number of fragment symbols to be deleted.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S : String;

begin

  // 1. Original string
  S := 'ABCDEF';

  // 2. Delete fragment 'CD'
  delete(S, 3, 2); // S = 'ABEF'
  Writeln(S);

  // 3. Set a new string.
  S := '123456789';

  // 4. Delete fragment '4'
  delete(S, 4, 1);
  Writeln(S);

  Readln;
end.

Program result

ABEF
12356789

 

4. The Insert procedure. Add a substring to a string

To insert another substring into a given string, use the Insert procedure. In general, the use of this procedure is as follows

Insert(S, S_new, Start);

here

  • S – string value – the fragment to be inserted;
  • S_new – string value into which the fragment S is inserted and where the result of the insertion is obtained;
  • Start is the ordinal number of the character in the S_new string, before which the S fragment is inserted. The Start value is numbered from 1.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S1, S2, S3: String;

begin

  // Procedure Insert() - insert a substring into a string

  // 1. Insert at the beginning of a string
  S1 := 'ABCD';
  Insert('000', S1, 1);
  Writeln(S1);

  // 2. Insert in the middle of a string
  S1 := '12345678'; // Original string
  S2 := 'ABC'; // String to be inserted

  // Calling the procedure Insert
  Insert(S2, S1, 3); // S1 = 12ABC345678
  Writeln(S1);

  // 3. Adding to the end of string
  S1 := '12345678';
  Insert('ABCD', S1, 9); // S1 = 12345678ABCD
  Writeln(S1);

  Readln;
end.

Program result

000ABCD
12ABC345678
12345678ABCD

 

5. Function Length. Get the current length of a string

The standard Length function is used to determine the length of a string. In the most general case, the use of the function can be as follows

Len := Length(S);

here

  • S – string of type String;
  • Len is the number of characters in the string S (length of the string).

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S : String;
  Len : Integer;

begin
  // Length() function - get the length of a string
  S := 'Hello, world!';
  Len := Length(S); // Len = 13
  Writeln(Len);

  Len := Length(''); // Len = 0
  Writeln(Len);

  Readln;
end.

Program result

13
0

 

6. Function Pos. Search for a substring in a string

To determine the location of the desired fragment of a string value, you can use the function

position := pos(S_find, S);

here

  • S_find – searched substring;
  • S – a string;
  • Position – the position from which the substring S_find begins in the string S. The Position value is of the Byte type and is numbered from 1. If the searched substring does not exist in the string, then Position = 0.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S : String;
  Position : Byte;

begin
  // Pos() function - determine the position of occurrence of a substring in a string

  // 1. The substring is in the string
  S := '123456789';
  Position := Pos('345', S); // Position = 3
  Writeln(Position);

  // 2. There are no substrings in the string
  Position := Pos('ABCD', 'ABC'); // Position = 0
  Writeln(Position);

  // 3. There are multiple occurrences of a substring in a string
  Position := Pos('AB', 'ABCD ABC ABD BAB'); // Position = 1
  Writeln(Position);

  Readln;
end.

Program result

3
0
1

 

7. Procedure Val. Convert string to a number

The Val procedure allows you to convert a string to a number. In this case, the string is considered to be a symbolic representation of a number (for example, ‘2.85’, ‘1879’). This procedure performs the inverse operation with respect to the procedure Str.
In the most general case, the use of the Val procedure has the form:

Val(S, V, ErrCode);

here

  • S – string value;
  • V – the value of the numeric type that needs to be converted;
  • ErrCode – code of the result of the procedure execution. If the conversion was successful, then ErrCode=0. If it is impossible to get the number V from the string S, then ErrCode is equal to the number of the position in the string S that cannot be converted. For example, for the string ‘123A56’, calling the Val procedure will return ErrCode = 4.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S : String;
  V1 : Integer;
  V2 : Real;
  Code : Integer;

begin

  // Procedure Val,
  // 1. Convert String => Integer
  S := '286';
  Val(S, V1, Code);

  // Process the result
  if Code = 0 then
    Writeln('Ok! V1 = ', V1)
  else
    Writeln('Error! ErrCode = ', Code);

  // 2. Convert String => Real
  S := '-38.25';
  Val(S, V2, Code);

  // Process the result
  if Code = 0 then
    Writeln('Ok! V2 = ', V2)
  else
    Writeln('Error! ErrCode = ', Code);

  // 3. Attempting to convert a string containing invalid characters
  S := '18U7'; // invalid characters here

  Val(S, V2, Code); // Code = 3

  if Code = 0 then
    Writeln('Ok! V2 = ', V2)
  else
    Writeln('Error! ErrCode = ', Code);

  Readln;
end.

Program result

Ok! V1 = 286
Ok! V2 = -3.82500000000000E+0001
Error! ErrCode = 3

 

8. Procedure Str. Convert a number to a string

The Str procedure is the inverse of the Val procedure and allows you to convert a value of a numeric type (Integer, Real, etc.) into a string of type String. General form of using the procedure

Str(V, S);

where

  • V – a value of a numeric type, the value of which is converted to a string form;
  • S – the result of a string type conversion.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  S : String;
  V1 : Integer;
  V2 : Real;

begin

  // Procedure Str,
  // 1. Convert Integer => String
  V1 := 277;
  Str(V1, S); // S = '277'
  Writeln('S = ', S);

  // 2. Convert Real => String
  V2 := 8.53;
  Str(V2, S);
  Writeln('S = ', S);

  Readln;
end.

Program result

S = 277
S = 8.53000000000000E+0000

 


Related topics