Main Index : Reference :

strstr()


Description

Locates the first occurrence of a phrase within another string, optionally beginning the search at a certain position.

Definition

[int] strstr([string] str,[string] phrase)
[int] strstr([string] str,[string] phrase,[int] pos)

Parameters

str is the string.
phrase is the search phrase.
pos is the optional start position of the search.

Return Value

strstr() returns the position of the first occurence of the string phrase within the string str. If the third parameter is passed, only the part of str that is after the position pos is searched. If no match is found, the return value is -1.

Remember: The first character of a string is at position 0.

Example

// Outputs "2". The phrase is located at position 2.

var str =  "abcdefghij";
var phrase = "cde";

println(strstr(str,phrase));


// Outputs "-1". No occurrence was found.

var str = "abcdefghij";
var phrase = "xgj";

println(strstr(str,phrase));


// Outputs "15". After position 8, the first occurrence 
// of the phrase is located at position 15.

var str    = "abcdefghij abcdefghij";
var phrase =                "efg";
var pos    = 8;

println(strstr(str,phrase,pos));

See Also

[string], strins(), strmid(), stradd(), strset(), strchr(), strcmp()