Core Function InStr

From Sputnik Wiki
Jump to: navigation, search
InStr( <string>, <substirng>, <casesense>, <occurrence>, <start>, <length> )

Contents

Description

Checks if a string contains a given substring.

Parameters

string

The string to evaluate.

substring

The substring to search for.

ignoreCase

Optional; Flag to indicate if the operations should be case insensitive.

true = not case sensitive

false = case sensitive

Default: true

occurrence

Optional; Which occurrence of the substring to find in the string. Use a negative occurrence to search from the right side.

The default value is 1 (finds first occurrence).

start

Optional; The starting position of the search.

OR

If the start is a negative value the character position will work backwards from the length of the string.

Default: 0

length

Optional; The number of characters to search. By default the entire remainder of the string.

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, -1 will be returned.

Return Value

Success: Returns the position of the substring.

Failure: Returns -1.

Remarks

The first character position is 0.

Example

$location = InStr("How much wood could a woodchuck chuck is a woodchuck could chuck wood?", "wood", 0, 3);
Println("Search result:" , $location); // Find the 3rd occurance of "wood"
 
$result = InStr("I am a String", "RING");
Println("Search result:" , $result);
// PRINTS
// Search result:43
// Search result:9

Using negative start and then using a length

$result = InStr("I am a String", "RING", true, 1, -4, 4);
Println("Search result:" , $result);
if ($result != -1)
	say "Found: " . SubStr("I am a String", $result, 4);
else
	say "NOT Found";
$result = InStr("I am a StrIng catchRing", "RING", true, 2, -16);
Println("Search result:" , $result);
if ($result != -1)
	say "Found: " . SubStr("I am a StrIng catchRing", $result, 4);
else
	say "NOT Found";
// PRINTS
// Search result:9
// Found: ring
// Search result:19
// Found: Ring
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox