Difference between revisions of "$len"
From Scriptwiki
m |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | Returns the length of | + | Returns the length of a string, i.e. how many characters are within it. |
− | |||
$len(text) | $len(text) | ||
− | $len( | + | $len() is a simple, but powerful tool for processing strings. |
− | + | == Examples == | |
− | + | $len(test string) returns 11. | |
− | + | The following example turns uppercase letters to lowercase and vice-versa. | |
− | <br/>$len is used to get | + | <br/>$len is used to get the length of the string (the value given to the alias) and loop through its every character. |
[[Aliases|alias]] changecase { | [[Aliases|alias]] changecase { | ||
Line 17: | Line 16: | ||
%c = [[$mid]]($1-, %i, 1) | %c = [[$mid]]($1-, %i, 1) | ||
[[If-Then-Else|if]] ([[$isupper]](%c)) { | [[If-Then-Else|if]] ([[$isupper]](%c)) { | ||
− | %str = %str [[$+]] [[$lower]](%c) | + | %str = %str [[DollarPlus|$+]] [[$lower]](%c) |
} | } | ||
[[If-Then-Else|else]] { | [[If-Then-Else|else]] { | ||
Line 29: | Line 28: | ||
[[echo]] -a $changecase(Hello) | [[echo]] -a $changecase(Hello) | ||
; returns hELLO | ; returns hELLO | ||
+ | |||
+ | == See Also == | ||
+ | |||
+ | * [[$mid]] - returns parts of a string | ||
+ | * [[$str]] - repeats a string | ||
+ | |||
[[Category:Text_and_Number_Identifiers]] | [[Category:Text_and_Number_Identifiers]] |
Latest revision as of 19:00, 25 April 2008
Returns the length of a string, i.e. how many characters are within it.
$len(text)
$len() is a simple, but powerful tool for processing strings.
Examples
$len(test string) returns 11.
The following example turns uppercase letters to lowercase and vice-versa.
$len is used to get the length of the string (the value given to the alias) and loop through its every character.
alias changecase { var %i = 1, %str, %c while (%i <= $len($$1-)) { %c = $mid($1-, %i, 1) if ($isupper(%c)) { %str = %str $+ $lower(%c) } else { %str = %str $+ $upper(%c) } inc %i } return %str } echo -a $changecase(Hello) ; returns hELLO