Official Content

Searches for a string within another one. 

Syntax

str1.IndexOf(str2 [, start-position ])

Where:

str1
    String on which the search will be done. It may be based on the Character data type, Varchar or LongVarchar data type.

str2
    Searched string. It may be based on the Character data type, Varchar or LongVarchar data type.

start-position
    Optional. It indicates the search start position. It must be a positive number (fixed value or variable).

Type Returned:
Numeric

Scope

Data Types: CharacterVarCharLongVarChar
Generators: .NET, .NET Framework, Java, Ruby (up to GeneXus X Evolution 3), Visual FoxPro (up to GeneXus X Evolution 3)

Description

The IndexOf method searches for a string within another one.

It returns the position (integer index starting in 1) of the str1 string where the str2 string was found. It searches from front to back.

The starting position is an optional parameter in the search. If it is omitted, the starting position will be position 1 of the str1 string. It must be a positive number. If it is 0, the method returns 0.

Notes:

  • The comparison between characters is case sensitive (i.e.:  'a' is different from 'A').
  • If str1 is empty or the length is shorter than str2, the method returns 0, since the search cannot be carried out.
  • If str2 is empty, it returns start-position if it was initialized; otherwise, it returns 1. 

Samples

&str1 ="StrSearch function tests"
&Number = &str1.IndexOf("j")    // Returns 0 ("j" not found)
&Number = &str1.IndexOf("a")    // Returns 6
&Number = &str1.IndexOf("e", 7) // Returns 21

See Also

IndexOf method - SDT Collection
StrReplace function


Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant