There existed essential functions of Pascal that should work with string operations. The following are the various string functions.
- Pos
- Copy
- Delete
- Insert
- UpCase
- Concat
The
Pos
function will scan for the string SubString inside the string S. On the off chance that the sub-string is not discovered, then 0 will be returned by the function. In the event that then again the sub-string is discovered, then the file whole number estimation of the principal character of the primary string that matches the character of the sub-string and it will be returned. The following is an example.
[c]program exObjects;
Var
S : String;
Begin
S := 'Hey there! How are you?';
Write('The word "How" is found at char index ');
Writeln(Pos('How', S));
If Pos('Why', S) <= 0 Then
Writeln('"Why" is not found.');
End.
[/c]
Output:The following is the result.
[c]The word "How" is found at char index 12
"Why" is not found.[/c]
The
copy
function will duplicate a few characters from string S beginning from character record Index. The following is an example.
[c]Var
S : String;
Begin
S := 'Hey there! How are you?';
S := Copy(S, 5, 6); { 'there!' }
Write(S);
End.
[/c]
Output:The following is the result.
[c]there![/c]
The
delete
function erases a predetermined number of characters from the string. The following is an example.
[c]Var
S : String;
Begin
S := 'Hey Max! How are you?';
Delete(S, 4, 4); { 'Hey! How are you?' }
Write(S);
End.[/c]
Output:The following is the result.
[c]Hey! How are you?[/c]
The
insert
function is used to insert the string. The following is an example.
[c]Var
S : String;
Begin
S := 'Hey! How are you?';
Insert(S, ' Max', 4);
Write(S);
{ 'Hey Max! How are you?' }
End.[/c]
Output:The following is the result.
[c]Hey Max! How are you?[/c]
The
UpCase
function is used to change the given string into upper case. The following is an example.
[c]Var
S : String;
i : Integer;
Begin
S := 'Hey! How are you?';
For i := 1 to length(S) do
S[i] := UpCase(S[i]);
Write(S); { 'HEY! HOW ARE YOU?' }
End.[/c]
Output:The result will be as follows.
[c]HEY! HOW ARE YOU?[/c]
The
concat
function is used to combine the both given string, the following is an example.
[c]Var
S1, S2 : String;
Begin
S1 := 'Hey!';
S2 := ' How are you?';
Write(S1 + S2); { 'Hey! How are you?' }
End.[/c]
Output: Now the result will be as follows.
[c]Hey! How are you?[/c]