Replacing substrings -
MP2 - 24.08.2011
For example, if someone says '$$$' I want to replace that with '$1234' - their money.
I need this to be easily implemented into any string, so i can use it for OnPlayerText, /pm etc.
Any and all help is much appreciated
Re: Replacing substrings -
[HiC]TheKiller - 24.08.2011
pawn Code:
stock str_replace(currentstr[], replacestr[], findstr[])
{
while(strfind(currentstr, findstr) != -1)
{
new pos = strfind(currentstr, findstr), len = strlen(replacestr);
strdel(currentstr, pos, pos+len);
strins(currentstr, replacestr, pos);
}
return 1;
}
public OnPlayerText(playerid, text[])
{
str_replace(text, "$1234", "$$$");
return 1;
}
Give that a go. Sorry it's a bit inefficient, I made it really fast.
Re: Replacing substrings -
MP2 - 25.08.2011
Could you also make it work if they said '$$$ $$$'?
Re: Replacing substrings -
Kingunit - 25.08.2011
Try editing every '$$$' to '$$$ $$$"
Re: Replacing substrings -
=WoR=G4M3Ov3r - 25.08.2011
Quote:
Originally Posted by MP2
Could you also make it work if they said '$$$ $$$'?
|
What do you mean ?, If someone says $$$ $$$ you wanna replace it with what ?
Re: Replacing substrings -
Davz*|*Criss - 25.08.2011
Do you mean if player say $$$ it should show his money to all?
I mean sendclientmessagetoall?
Re: Replacing substrings -
MP2 - 25.08.2011
Never mind I think his will work.
Re: Replacing substrings -
MP2 - 25.08.2011
It doesn't work. Can anyone else help please? I want '$$$' to be replaced with $1234 no matter how many times it appears in a string, so if someone says '$$$ $$$ $$$' it will change to '$1234 $1234 $1234'
Thanks
Re: Replacing substrings -
RyDeR` - 25.08.2011
http://forum.sa-mp.com/showpost.php?...postcount=2107
Re: Replacing substrings -
=WoR=G4M3Ov3r - 25.08.2011
PHP Code:
main()
{
new
sOriginal[256],
sNewString[256]
strins(sOriginal, "$$$", 0);
sNewString = str_replace("$$$", "$1234", sOriginal)
printf("New str: %s", sNewString)
}
Re: Replacing substrings -
MP2 - 25.08.2011
Is there not a way to use this in OnPlayerText without creating a new 128 string?
Re: Replacing substrings -
Drako1407 - 25.08.2011
to me , give me this waring..
pawn Code:
C:\Users\Franco\Desktop\samp03csvr_R2-2_win32\gamemodes\Bk.pwn(9065) : warning 224: indeterminate array size in "sizeof" expression (symbol "maxlength")
whait?
line:
pawn Code:
stock str_replace(currentstr[], replacestr[], findstr[])
{
while(strfind(currentstr, findstr) != -1)
{
new pos = strfind(currentstr, findstr), len = strlen(replacestr);
strdel(currentstr, pos, pos+len);
strins(currentstr, replacestr, pos);
}
return 1;
}
Re: Replacing substrings -
RyDeR` - 25.08.2011
Quote:
Originally Posted by MP2
Is there not a way to use this in OnPlayerText without creating a new 128 string?
|
As long as you are sure that the maximum length won't exceed the array bounds, then yes.