From Clomosy Docs

No edit summary
No edit summary
Line 11: Line 11:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
<pre>
  var
  var
Line 27: Line 27:
     ShowMessage('Substring not found.');
     ShowMessage('Substring not found.');
  }
  }
</pre>
<b>Base Syntax</b><br>
<pre>
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;
</pre>
</pre>


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 25 (the starting position).
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 25 (the starting position).<br>




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.
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.<br>


<b>Output:</b><br>
<b>Output:</b><br>

Revision as of 12:31, 13 November 2024

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.

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.

Example

 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 25 (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.

Output:

See Also