From Clomosy Docs

Revision as of 13:11, 13 February 2024 by ClomosyManager (talk | contribs)

In the Clomosy programming language, the Pos function allows you to find the position of a specific substring within another string. This function searches for a given substring within a target string and returns its position. The usage of the Pos function is as follows:

function Pos(const SubStr: string; const Str: string): Integer;

Here:

SubStr: The substring to be searched for. Str: The main string in which the search will be performed. Function result: Returns the position of the substring found (starting from 1). If the substring is not found, it returns a value of 0.

Example:

Base Syntax
var
  MainString, SubString: string;
  Position: Integer;
begin
  MainString := 'Hello, you are learning Clomosy.';
  SubString := 'Clomosy';

  Position := Pos(SubString, MainString);

  if Position > 0 then
    ShowMessage('Substring found. Position: ' + 
IntToStr(Position))
  else
    ShowMessage('Substring not found.');
end;
TRObject Syntax
 var
   MainString, SubString: string;
   Position: Integer;
 {
   MainString = 'Hello, you are learning Clomosy.';
   SubString = 'Clomosy';
 
   Position = Pos(SubString, MainString);
 
   if (Position > 0)
     ShowMessage('Substring found. Position: ' + IntToStr(Position));
   else
     ShowMessage('Substring not found.');
 }


In this example, the substring "Clomosy," which is SubString, is found within the string "Hello, you are learning Clomosy," which is MainString. As a result, it will return 10 (the starting position).


The Pos function returns only the position of the first occurrence of the substring. If you want to find the substring multiple times, you may need to use loops or different approaches.