mIRC Homepage
Posted By: lostay $replace but only part of the string - 09/02/16 08:26 PM
Okay, let's say I have this:

Code:
var %x = I like bots, bananas, solutions.


If I want to replace only the second comma to " and" or something, how would I do that?

If I do:
Code:
$replace(%x,$chr(44), $chr(32) and)


The result will be: I like bots and bananas and solutions.
But I want it to be: I like bots, bananas and solutions.

Of course, I could just split the string into two, but that's not what I'm looking for (I want it to be able to do it with every string I give him)

Thanks in advance.
Posted By: westor Re: $replace but only part of the string - 09/02/16 08:31 PM
Try use this method:

You will need $gettok (/help $gettok)

Code:
//var %text = I like bots, bananas, solutions. | echo -a $replace(%text,$gettok(%text,2,44) $+ $chr(44),$chr(32) and)
Posted By: Loki12583 Re: $replace but only part of the string - 09/02/16 08:47 PM
How does splitting the string prevent you from doing it generically?

All but last item: $gettok(%x,-2-1,44)
Last item: $gettok(%x,-1,44)
Posted By: hixxy Re: $replace but only part of the string - 01/03/16 04:45 PM
Code:
$deltok(%x,-1,44) and $gettok(%x,-1,44)


Code:
//var %x = I like bots, bananas, solutions. | echo -a $deltok(%x,-1,44) and $gettok(%x,-1,44)


Output:

"I like bots, bananas and solutions."
Posted By: Wims Re: $replace but only part of the string - 01/03/16 04:57 PM
Tell us about what you don't like in $gettok(,-2-1,C) hixxy, cause it must be worth bumping this thread grin
Posted By: hixxy Re: $replace but only part of the string - 01/03/16 05:00 PM
Sure: it doesn't work! Have you tried it? :p
Posted By: Wims Re: $replace but only part of the string - 01/03/16 05:04 PM
I didn't but I just did now

Code:
//var %text = I like bots, bananas, solutions. | echo -a $gettok(%text,-2-1,44) and $gettok(%text,-1,44)
Posted By: hixxy Re: $replace but only part of the string - 01/03/16 05:06 PM
It does work actually. My bad, I was using it on an 'a, b and c.' string rather than 'a, b, c.'.
Posted By: Wims Re: $replace but only part of the string - 01/03/16 05:09 PM
eheh, you could have argued that $deltok was shorter!
© mIRC Discussion Forums