From Clomosy Docs

No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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:
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
function Pos(const SubStr: string; const Str: string): Integer;
</div>
Here:


function Pos(const SubStr: string; const Str: string): Integer;
:<b>SubStr:</b> The substring to be searched for.
:<b>Str:</b> The main string in which the search will be performed.
:<b>Function result:</b> Returns the position of the substring found (starting from 1). If the substring is not found, it returns a value of 0.<br>


Here:
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.


SubStr: The substring to be searched for.
<b>Example</b><br>
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:'''<br>
<pre>
:'''Base Syntax'''
  var
  var
   MainString, SubString: string;
   MainString, SubString: string;
   Position: Integer;
   Position: Integer;
  begin
  {
   MainString := 'Hello, you are learning Clomosy.';
   MainString = 'Hello, you are learning Clomosy.';
   SubString := 'Clomosy';
   SubString = 'Clomosy';
   
   
   Position := '''Pos'''(SubString, MainString);
   Position = Pos(SubString, MainString);
   
   
   if Position > 0 then
   if (Position > 0)
     ShowMessage('Substring found. Position: ' +  
     ShowMessage('Substring found. Position: ' + IntToStr(Position));
IntToStr(Position))
   else
   else
     ShowMessage('Substring not found.');
     ShowMessage('Substring not found.');
  end;
  }
 
</pre>
:'''TRObject Syntax'''


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


<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
Substring found. Position: 25
</div>


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.
<h2> See Also </h2>
* [[System_Library#String_Functions | String Functions]]
{{#seo:|title=Pos Using in Clomosy - Clomosy Docs}}
{{#seo:|description=Learn how the Pos function in Clomosy finds the position of a substring within a string, returning the starting position or 0 if not found.}}

Latest revision as of 13:34, 24 December 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