Difference between revisions of "$mid"
From Scriptwiki
m (added parent category, few typos) |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Returns '''I''' amount of | + | Returns '''I''' amount of characters from the '''N'''th character in '''text'''. |
$mid(<text>,N[,I]) | $mid(<text>,N[,I]) | ||
− | I is optional, if I is | + | I is optional, if I is omitted, it will return from position N onwards. |
== Examples == | == Examples == | ||
var %i = abcde | var %i = abcde | ||
echo -ag $mid(%i,2,3) | echo -ag $mid(%i,2,3) | ||
− | This above example will echo out bcd. This is because it grabs the text from position ''N'' which is ''2'' in this case, ''a'' being ''1'', and ''b'' being ''2''. From this spot, we now grab then next ''I'' | + | This above example will echo out bcd. This is because it grabs the text from position ''N'' which is ''2'' in this case, ''a'' being ''1'', and ''b'' being ''2''. From this spot, we now grab then next ''I'' characters (''3'') so we grab ''b=1 c=2 d=3'' |
This is an example to loop threw every character and check it against something. | This is an example to loop threw every character and check it against something. | ||
− | '';Set the | + | '';Set the variable %text to the string we are going to use.'' |
− | [[var]] %text = There | + | [[var]] %text = There are 14 brown cows, they're hyperactive and jump over the 6 lazy red fish. :-( |
− | '';Set the | + | '';Set the variable %n to 1 and %i to the length of %text.'' |
var %n = 1, %i = [[$len]](%text) | var %n = 1, %i = [[$len]](%text) | ||
− | '';Creates these local | + | '';Creates these local variables so they don't save as global when used.'' |
var %letters = 0, %numbers = 0, %spaces = 0, %other = 0 | var %letters = 0, %numbers = 0, %spaces = 0, %other = 0 | ||
'';Keep looping while %n is less than or equal to %i.'' | '';Keep looping while %n is less than or equal to %i.'' | ||
[[While loops|while]] (%n [[If-Then-Else#The_Operators|<=]] %i) { | [[While loops|while]] (%n [[If-Then-Else#The_Operators|<=]] %i) { | ||
− | '';Set %letter to the | + | '';Set %letter to the character located at %n, only grab 1 character.'' |
var %letter = $mid(%text,%n,1) | var %letter = $mid(%text,%n,1) | ||
− | '';If %letter is a letter, number, space or other set the | + | '';If %letter is a letter, number, space or other set the corresponding variable.'' |
[[If-Then-Else|if]] (%letter [[If-Then-Else#The_Operators|isletter]]) { [[inc]] %letters } | [[If-Then-Else|if]] (%letter [[If-Then-Else#The_Operators|isletter]]) { [[inc]] %letters } | ||
[[If-Then-Else|elseif]] (%letter [[If-Then-Else#The_Operators|isnum]]) { inc %numbers } | [[If-Then-Else|elseif]] (%letter [[If-Then-Else#The_Operators|isnum]]) { inc %numbers } | ||
Line 29: | Line 29: | ||
[[If-Then-Else|else]] { inc %other } | [[If-Then-Else|else]] { inc %other } | ||
− | '';Increment %n by 1, so first loop %n = 1, then %n = 2 | + | '';Increment %n by 1, so first loop %n = 1, then %n = 2 until %n is greater than %i'' |
inc %n | inc %n | ||
} | } | ||
[[echo]] -ag String: %text | [[echo]] -ag String: %text | ||
− | echo -ag There | + | echo -ag There are %letters letters, %numbers numbers, %spaces spaces and %other other characters (punctuations etc.) |
== Also See == | == Also See == | ||
* [[$right]] | * [[$right]] | ||
* [[$left]] | * [[$left]] | ||
+ | [[Category:Text_and_Number_Identifiers]] |
Latest revision as of 01:45, 18 February 2006
Returns I amount of characters from the Nth character in text.
$mid(<text>,N[,I])
I is optional, if I is omitted, it will return from position N onwards.
Examples
var %i = abcde echo -ag $mid(%i,2,3)
This above example will echo out bcd. This is because it grabs the text from position N which is 2 in this case, a being 1, and b being 2. From this spot, we now grab then next I characters (3) so we grab b=1 c=2 d=3
This is an example to loop threw every character and check it against something.
;Set the variable %text to the string we are going to use. var %text = There are 14 brown cows, they're hyperactive and jump over the 6 lazy red fish. :-( ;Set the variable %n to 1 and %i to the length of %text. var %n = 1, %i = $len(%text) ;Creates these local variables so they don't save as global when used. var %letters = 0, %numbers = 0, %spaces = 0, %other = 0 ;Keep looping while %n is less than or equal to %i. while (%n <= %i) { ;Set %letter to the character located at %n, only grab 1 character. var %letter = $mid(%text,%n,1) ;If %letter is a letter, number, space or other set the corresponding variable. if (%letter isletter) { inc %letters } elseif (%letter isnum) { inc %numbers } elseif (%letter == $chr(32)) { inc %spaces } else { inc %other } ;Increment %n by 1, so first loop %n = 1, then %n = 2 until %n is greater than %i inc %n } echo -ag String: %text echo -ag There are %letters letters, %numbers numbers, %spaces spaces and %other other characters (punctuations etc.)