Core Function Strpbrk
(→start) |
|||
(4 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | Strpbrk( <haystack>, <needle>, <start> ) | + | Strpbrk( <haystack>, <needle>, <start>, <count> ) |
</pre> | </pre> | ||
Line 21: | Line 21: | ||
Optional; Start position to begin searching the haystack from. | Optional; Start position to begin searching the haystack from. | ||
− | + | If the start is a negative value the character position will work backwards from the length of the string. | |
Default: 0 | Default: 0 | ||
+ | |||
+ | ==== count ==== | ||
+ | |||
+ | Optional; The number of characters to search. By default the entire remainder of the string. | ||
+ | |||
+ | If count 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, NULL will be returned. | ||
=== Return Value === | === Return Value === | ||
Line 63: | Line 69: | ||
// this echoes "Simple text." because chars are case sensitive | // this echoes "Simple text." because chars are case sensitive | ||
say substr($text, strpbrk($text, 'S')); | say substr($text, strpbrk($text, 'S')); | ||
− | / | + | </syntaxhighlight> |
− | // | + | |
+ | Example of using negative start position | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // this echoes ";Cat" since -4 starts from 4 letters from end of the string | ||
+ | $text = "Hello;Cat"; | ||
+ | say substr($text, strpbrk($text, ';', -4)); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 20:47, 16 September 2015
Strpbrk( <haystack>, <needle>, <start>, <count> )
Contents |
Description
Locate a list of possible characters in string and return the position of it.
Parameters
haystack
The string to search in.
needle
The string containing the characters to match.
start
Optional; Start position to begin searching the haystack from.
If the start is a negative value the character position will work backwards from the length of the string.
Default: 0
count
Optional; The number of characters to search. By default the entire remainder of the string.
If count 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, NULL will be returned.
Return Value
Position of the first occurrence in the haystack of any of the characters that are part of needle.
If none of the characters of needle is present in haystack, a null is returned.
Remarks
None.
Example
my $str = "This is a sample string"; my $key = "aeiou"; printf ("Vowels in '%s': ",$str); my $pch = strpbrk ($str, $key); while ($pch !== NULL) { printf ("%c " , $str[$pch]); $pch = strpbrk( $str, $key, $pch+1 ); } printf ("\n"); return 0; // Prints // Vowels in 'This is a sample string': i i a a e i
You may want to return the string after the match
$text = 'This is a Simple text.'; // this echoes "is is a Simple text." because 'i' is matched first say substr($text, strpbrk($text, 'mi')); // this echoes "Simple text." because chars are case sensitive say substr($text, strpbrk($text, 'S'));
Example of using negative start position
// this echoes ";Cat" since -4 starts from 4 letters from end of the string $text = "Hello;Cat"; say substr($text, strpbrk($text, ';', -4));