$mid

From Scriptwiki
Revision as of 12:48, 20 December 2005 by Doomie (talk | contribs)

Jump to: navigation, search

Returns I amount of charaters from the Nth charater in text.

$mid(<text>,N[,I])

I is optional, if I is ommited, 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 charaters (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 charater located at %n, only grab 1 charater.
  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 charaters (punctuations etc.)

Also See