SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 11/01/2019, 02:26 PM   #1
NoteND
Big Clucker
 
Join Date: May 2016
Posts: 93
Reputation: 0
Default strreplace problem

Why does this strreplace(path, " ", "_"); give me an error error 035: argument type mismatch (argument 2)

stock strreplace(string[], find, replace)
{
for(new i=0; string[i]; i++)
{
if(string[i] == find)
{
string[i] = replace;
}
}
}

Basically wanna check if there's space and replace it with "_"
NoteND is offline   Reply With Quote
Old 11/01/2019, 02:39 PM   #2
Macronix
Huge Clucker
 
Macronix's Avatar
 
Join Date: Jul 2010
Location: Germany
Posts: 436
Reputation: 82
Default Re: strreplace problem

Use this:

pawn Code:
stock str_replace(sSearch[], sReplace[], const sSubject[], &iCount = 0)
{
    new
        iLengthTarget = strlen(sSearch),
        iLengthReplace = strlen(sReplace),
        iLengthSource = strlen(sSubject),
        iItterations = (iLengthSource - iLengthTarget) + 1;

    new
        sTemp[128],
        sReturn[_strlib_med_string];

    strcat(sReturn, sSubject, _strlib_med_string);
    iCount = 0;

    for(new iIndex; iIndex < iItterations; ++iIndex)
    {
        strmid(sTemp, sReturn, iIndex, (iIndex + iLengthTarget), (iLengthTarget + 1));

        if(!strcmp(sTemp, sSearch, false))
        {
            strdel(sReturn, iIndex, (iIndex + iLengthTarget));
            strins(sReturn, sReplace, iIndex, iLengthReplace);

            iIndex += iLengthTarget;
            iCount++;
        }
    }

    return sReturn;
}

Arguments:
sSearch[]: String to search for
sReplace[]: String to replace with
sSubject[]: Original string
&iCount: How many times 'sSearch' has been replaced. (optional)

Code:
str_replace(" ", "_", path);
Source: https://forum.sa-mp.com/showthread.php?t=85697
Macronix is offline   Reply With Quote
Old 11/01/2019, 02:42 PM   #3
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 15,518
Reputation: 3207
Default Re: strreplace problem

Why is the parameter to be modified `const`, while the ones that don't change not? You have also clearly taken that from a library but not mentioned where, which is bad for three reasons:

1) You aren't crediting the original author.
2) It uses variables you haven't copied or defined.
3) Why not just use the whole library?
Y_Less is online now   Reply With Quote
Old 11/01/2019, 02:54 PM   #4
Macronix
Huge Clucker
 
Macronix's Avatar
 
Join Date: Jul 2010
Location: Germany
Posts: 436
Reputation: 82
Default Re: strreplace problem

Of course, my bad, sorry. Edited the post
Guess i was a bit too lazy there, lol.
Macronix is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strreplace problem ShoortyFl Scripting Help 1 20/07/2015 04:52 PM
strreplace Sellize Scripting Help 1 07/08/2013 01:27 PM
strreplace isn't working for me vvhy Scripting Help 7 21/06/2012 08:50 PM
strreplace problem antonio600x Scripting Help 5 30/08/2011 01:54 PM
String / strreplace Problem [URGENT] Kyle Help Archive 7 28/04/2010 07:16 PM


All times are GMT. The time now is 08:33 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.