Snippets Project Page
Author: Imk0tter
Added: 2y
Updated: 2y
mIRC: 7.64
Hits: 540
Downloads: 5
Review: westor
Size: 2.59KB
0 0
Login to vote.
$replaceTok(string,substr,n,token,[substr],[n],[token],...)
v2.0
Here is an alias that allows you to replace the n'th occurence of the specified substring in the specified string with a token.
IE: $replaceTok(this @ is @ a @ test, @, -1, ---,-,-1,^) = this @ is @ a --^test
IE: $replaceTok(this @ is @ a @ test, @, -1, ---,-,-2,^) = this @ is @ a -^- test
IE: $replaceTok(this @ is @ a @ test, @, -1, ---,-,1,^) = this @ is @ a ^-- test
IE: $replaceTok(this @ is @ a @ test, @, -2, ---,-,-2,^) = this @ is -^- a @ test
Download
JSON
▲ Review
▲ Log
▼ Source
alias replacetok { var %string $1 var %count 2 while %count < $0 { var %replaceText [ $ $+ [ %count ] ] inc %count var %max $pos(%string,%replaceText, 0) var %token $parseRange([ $ $+ [ %count ] ], %max) inc %count var %startTok $token(%token,1,32) var %endTok $token(%token,2,32) + 1 var %text [ $ $+ [ %count ] ] inc %count var %tokensReplaced 0 while %startTok < %endTok { var %pos $pos(%string,%replaceText,$calc(%startTok - %tokensReplaced)) var %string $+($left(%string,$calc(%pos - 1)),%text,$right(%string,- $+ $calc(%pos + $len(%replaceText) - 1))) inc %startTok inc %tokensReplaced } } return %string } alias -l parserange { ;parses range of up to 2 integers (which can be positive or negative) separated by a '-' var %input $1 var %maxRange $2 var %startNumber, %endNumber,%c $token(%input,1,45) ;if the first character is a -, the first parameter is a negative number if ($left(%input,1) == -) { ;first number is a negative number var %startNumber $calc(%maxRange - %c + 1) var %input $right(%input,-1) ;proceed to second - or end of the string } else { ;first integer is a non-negative integer var %startNumber %c ;proceed to first - or end of strimg } var %a $pos(%input,-,1) ;proceed to first (or second if the first integer was negative) '-' and if it is at the end of the string, or is before the second integer (the second integer is a negative number) if %a == $len(%input) { ;first (or second if the first integer was negative) '-' is at end of string (range encountered) no second integer %endNumber = %maxRange } else if !%a { ;no - after the first token; no range; end of string %endNumber = %startNumber } else { ;range encountered; second number present ;proceed to first - or end of string, and if there is a positive or negative integer var %a $iif($Pos(%input,-,1),$v1,0) var %b $iif($pos(%input,-,2),$v1,0) var %c $token(%input,2,45) if %b > 0 && $calc(%b - %a) == 1 { ;second integer is negative, var %endNumber $calc(%maxRange - %c + 1) } else if %b == 0 { ;second integer is positive var %endnumber %c } else { ;unknown condition + HELP } } return %startNumber %endNumber }
Changelog:
UPDATE: you can now specify more than one set of inputs: [substr],[n],[token]
(the code operates on the results of the previous set of inputs)
UPDATE: you can now specify more than one set of inputs: [substr],[n],[token]
(the code operates on the results of the previous set of inputs)
Comments (2)
May 10th, 2021 - By: Imk0tter
the previous version:
[code]
alias replacetok {
var %pos $pos($1,$2,$iif($3 > 0,$3,$calc($pos($1,$2,0) + $3 + 1)))
return $+($left($1,$calc(%pos - 1)),$$4,$right($1,- $+ $calc(%pos + $len($2) - 1)))
}
[/code]
[code]
alias replacetok {
var %pos $pos($1,$2,$iif($3 > 0,$3,$calc($pos($1,$2,0) + $3 + 1)))
return $+($left($1,$calc(%pos - 1)),$$4,$right($1,- $+ $calc(%pos + $len($2) - 1)))
}
[/code]
Reply:
Oct 22nd, 2022 - By: entropy
Super Admin
found an error:
else if %b == 0
needs to be: "elseif".
else if %b == 0
needs to be: "elseif".
Reply: