Core Function Chop
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> Chop( <variable> ) </pre> === Description === Removes and returns the last character from a string. === Parameters === ==== variable ==== Can be any variable including...") |
(→Example) |
||
(3 intermediate revisions by one user not shown) | |||
Line 25: | Line 25: | ||
=== Remarks === | === Remarks === | ||
− | + | This modifies the string in place. | |
=== Example === | === Example === | ||
Line 32: | Line 32: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $str = " | + | $str = "Hello"; |
print( "Removed '" . chop($str) . "'\n" ); | print( "Removed '" . chop($str) . "'\n" ); | ||
print( "Value: '$str'\n" ); | print( "Value: '$str'\n" ); |
Latest revision as of 22:24, 4 September 2015
Chop( <variable> )
Contents |
Description
Removes and returns the last character from a string.
Parameters
variable
Can be any variable including numbers, strings, arrays.
Note - If it finds an ARRAY it will do the chop() on every element in the array (but not the keys).
It will chop elements that are numeric, string or arrays it will not mess with objects etc.
Return Value
Success: The character that was removed.
Failure: empty string.
Remarks
This modifies the string in place.
Example
Remove last character from a string
$str = "Hello"; print( "Removed '" . chop($str) . "'\n" ); print( "Value: '$str'\n" ); # Prints # Removed 'o' # Value: 'Hell'
Remove last character from each element in an array
$flowers = qw(roses tulips violets snowdrops); chop($flowers); printr $flowers; # ARRAY # { # [0] => rose # [1] => tulip # [2] => violet # [3] => snowdrop # }
Remove last character from each element in an array that has keys
$flowers = qww(red roses yellow tulips white snowdrops); chop($flowers); printr $flowers; # ARRAY # { # [red] => rose # [yellow] => tulip # [white] => snowdrop # }