SA-MP Forums Archive
Replace ' ' with '_' - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Replace ' ' with '_' (/showthread.php?tid=523899)



Replace ' ' with '_' - Jack_Leslie - 04.07.2014

So i'm trying to make some sort of stock that replaces a space with an underscore, like the opposite of what you do if you have a roleplay server. Everything I've tried has failed, and everything i've found on ****** was failed me.


AW: Replace ' ' with '_' - Mellnik - 04.07.2014

pawn Код:
for(new i = 0, l = strlen(string); i < l; i++)
    {
        if(string[i] == ' ')
        {
            string[i] = '_';
        }
    }
?


Re: Replace ' ' with '_' - Jack_Leslie - 04.07.2014

pawn Код:
stock AddUnderscore(string[])
{
    for(new i = 0, l = strlen(string); i < l; i++)
    {
        if(string[i] == ' ')
        {
            string[i] = '_';
        }
    }
    return string;
}

format(string, sizeof(string), "%s", AddUnderscore(PlayerData[playerid][Fullname]));
printf("%s", string);
SetPlayerName(playerid, string);
Print:
Код:
[00:42:41] T
Hint: PlayerData[playerid][Fullname] = Jack Leslie
Needs to return Jack_Leslie


AW: Replace ' ' with '_' - Mellnik - 04.07.2014

Just tested it and it works for me. It looks like you can't return the parameter "string".

Working function for your scenario:
pawn Код:
AddUnderscore(string[])
{
    new manipulated[MAX_PLAYER_NAME];
    for(new i = 0, l = strlen(string); i < l; i++)
    {
        manipulated[i] = string[i];
        if(string[i] == ' ')
        {
            manipulated[i] = '_';
        }
    }
    return manipulated;
}
Btw, why did you add "stock" to the function?


Re: Replace ' ' with '_' - Rittik - 04.07.2014

Try this.

Код:
stock AddUnderscore(string[])
{
       str_replace(" ", "_", string);
       return string;
}
pawn Код:
stock str_replace(sSearch[], sReplace[], const sSubject[], &iCount = 0) //Credits to "Westie" for this function.
{
    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;
}



Re: AW: Replace ' ' with '_' - Jack_Leslie - 05.07.2014

Quote:
Originally Posted by Mellnik
Посмотреть сообщение
Just tested it and it works for me. It looks like you can't return the parameter "string".

Working function for your scenario:
pawn Код:
AddUnderscore(string[])
{
    new manipulated[MAX_PLAYER_NAME];
    for(new i = 0, l = strlen(string); i < l; i++)
    {
        manipulated[i] = string[i];
        if(string[i] == ' ')
        {
            manipulated[i] = '_';
        }
    }
    return manipulated;
}
Btw, why did you add "stock" to the function?
That worked a treat, thanks mate.