Replacing substrings
#1

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
Reply
#2

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.
Reply
#3

Could you also make it work if they said '$$$ $$$'?
Reply
#4

Try editing every '$$$' to '$$$ $$$"
Reply
#5

Quote:
Originally Posted by MP2
View Post
Could you also make it work if they said '$$$ $$$'?
What do you mean ?, If someone says $$$ $$$ you wanna replace it with what ?
Reply
#6

Do you mean if player say $$$ it should show his money to all?

I mean sendclientmessagetoall?
Reply
#7

Never mind I think his will work.
Reply
#8

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
Reply
#9

http://forum.sa-mp.com/showpost.php?...postcount=2107
Reply
#10

PHP Code:
main()
{
    new
        
sOriginal[256],
        
sNewString[256]
 
    
strins(sOriginal"$$$"0);
 
    
sNewString str_replace("$$$""$1234"sOriginal)
 
    
printf("New str:  %s"sNewString)

Reply
#11

Is there not a way to use this in OnPlayerText without creating a new 128 string?
Reply
#12

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;
}
Reply
#13

Quote:
Originally Posted by MP2
View Post
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)