Core Function InStr
(→casesense) |
|||
(One intermediate revision by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | InStr( <string>, <substirng>, <casesense>, <occurrence>, <start> | + | InStr( <string>, <substirng>, <casesense>, <occurrence>, <start>, <length> ) |
</pre> | </pre> | ||
Line 25: | Line 25: | ||
false = case sensitive | false = case sensitive | ||
− | Default: | + | Default: true |
==== occurrence ==== | ==== occurrence ==== | ||
Line 36: | Line 36: | ||
Optional; The starting position of the search. | 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 | 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 === | === Return Value === | ||
Line 43: | Line 53: | ||
Success: Returns the position of the substring. | Success: Returns the position of the substring. | ||
− | Failure: Returns | + | Failure: Returns -1. |
=== Remarks === | === Remarks === | ||
− | The first character position is | + | The first character position is 0. |
=== Example === | === Example === | ||
Line 57: | Line 67: | ||
$result = InStr("I am a String", "RING"); | $result = InStr("I am a String", "RING"); | ||
Println("Search result:" , $result); | Println("Search result:" , $result); | ||
+ | // PRINTS | ||
+ | // Search result:43 | ||
+ | // Search result:9 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Using negative start and then using a length | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | $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 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 07:51, 17 September 2015
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