Modifier

Partager via


Text.StrPos(Text, Text) Method

Version: Available or changed with runtime version 1.0.

Searches for the first occurrence of substring inside a string.

Syntax

Position :=   Text.StrPos(String: Text, SubString: Text)

Note

This method can be invoked without specifying the data type name.

Parameters

String
 Type: Text
The string in which you want to search.

SubString
 Type: Text
The substring for which you want to search.

Return Value

Position
 Type: Integer
The one-based index of the first occurrence of the substring inside the string.

Remarks

The StrPos method returns the position of the first occurrence of the substring.

If SubString cannot be found, then the method returns zero.

If String or SubString is empty, then the method returns zero.

Example 1

This example shows how to use the StrPos method.

var
    Text000: Label 'ABC abc abc xy';  
    Text001: Label 'abc'; 
    Text002: Label 'The search for the substring: >%1<\\'; 
    Text003: Label 'in the string: >%2<,\\'; 
    Text004: Label 'returns the position: %3';  
begin
    String := Text000;  
    SubStr := Text001  
    Pos := StrPos(String, SubStr);  
    Message(Text002 + Text003 + Text004, SubStr, String, Pos);  
    // The StrPos method is case-sensitive. Furthermore, it only  
    // returns the position of the 1st occurrence of the substring.  
end;

The message window displays the following:

The search for the substring: >abc<

in the string: >ABC abc abc xy<

returns the position: 5

Example 2

Pos1 := StrPos("abc",""); // Returns 0.  
Pos2 := StrPos("abc","c"); // Returns 3.  
Pos3 := StrPos("abc","bc"); // Returns 2.  
Pos4 := StrPos("abc","x"); // Returns 0.  

Text Data Type
Get Started with AL
Developing Extensions