Difference between revisions of "$reptok"

From Scriptwiki
Jump to: navigation, search
 
m (N=0 update)
 
Line 1: Line 1:
 
Replaces the Nth matching token in text with a new token.
 
Replaces the Nth matching token in text with a new token.
 
  $reptok(text,token,new,N,C)
 
  $reptok(text,token,new,N,C)
 +
If N is 0, replaces all matching tokens.
  
 
'''Note''' that $reptokcs() is the case-sensitive version.
 
'''Note''' that $reptokcs() is the case-sensitive version.
Line 8: Line 9:
 
  var %mynewtext = $reptok(%myoldtext, old, new, 1, 32)
 
  var %mynewtext = $reptok(%myoldtext, old, new, 1, 32)
 
  [[echo]] -a %mynewtext
 
  [[echo]] -a %mynewtext
This example echo's ''This is my new text'', as it replaced ''old'' with ''new''.
+
This example echoes ''This is my new text'', as it replaced ''old'' with ''new''.
  
 
  var %numbers = 4,5,6,6,8,9
 
  var %numbers = 4,5,6,6,8,9
 
  var %numbers = $reptok(%numbers,6,7,2,44)
 
  var %numbers = $reptok(%numbers,6,7,2,44)
 
  echo -a %numbers
 
  echo -a %numbers
This echo's ''4,5,6,7,8,9'', as it replaced the second ''6'' with ''7''.
+
This echoes ''4,5,6,7,8,9'', as it replaced the second ''6'' with ''7''.
 +
 
 +
var %numbers = 0,1,2,0,2,2,0,1,2,1,0
 +
echo -ag $reptok(%numbers,0,1,0,44)
 +
This echoes ''1,1,2,1,2,2,1,1,2,1,1'' as all 0's are replaced with the number 1 due to N being 0.
  
 
[[Category:Token Identifiers]]
 
[[Category:Token Identifiers]]

Latest revision as of 16:18, 23 May 2008

Replaces the Nth matching token in text with a new token.

$reptok(text,token,new,N,C)

If N is 0, replaces all matching tokens.

Note that $reptokcs() is the case-sensitive version.

Example

var %myoldtext = This is my old text
var %mynewtext = $reptok(%myoldtext, old, new, 1, 32)
echo -a %mynewtext

This example echoes This is my new text, as it replaced old with new.

var %numbers = 4,5,6,6,8,9
var %numbers = $reptok(%numbers,6,7,2,44)
echo -a %numbers

This echoes 4,5,6,7,8,9, as it replaced the second 6 with 7.

var %numbers = 0,1,2,0,2,2,0,1,2,1,0
echo -ag $reptok(%numbers,0,1,0,44)

This echoes 1,1,2,1,2,2,1,1,2,1,1 as all 0's are replaced with the number 1 due to N being 0.