Replacing "_" with an space in a string
#1

Basically, I'm trying to make a function based off GetPlayerName, but to remove the "_" and replace it with a space.

pawn Код:
stock GetPlayerRoleplayName(playerid)
{
    new playa[MAX_PLAYER_NAME],result[34];
    GetPlayerName(playerid,playa,sizeof(playa));
    if(strfind(playa,"_",true) != -1)
    {
        return result;
    }
}
Could anyone help me with the rest please?
Reply
#2

pawn Код:
stock RemoveSymbol(playerid) {
new nickname[24];
GetPlayerName(playerid, nickname, 24);
for ( new i = 0 , j = strlen( nickname ) ; i < j ; ++ i ) {
if( nickname[ i ] == "_" ) {
nickname[i] = ' ';
}
}
return nickname;
}
returns new nickname.. try this
Reply
#3

error 033: array must be indexed (variable "-unknown-")

Shouldnt the "if( nickname[ i ] == "_" )" use strfind?
Reply
#4

Put this anywhere in your gamemode:
Код:
stock strreplace(string[], find, replace)
{
    for(new i=0; string[i]; i++)
    {
        if(string[i] == find)
        {
            string[i] = replace;
        }
    }
}
Place this where you want it to replace the players '_' in its name:
Код:
new pname[24];
GetPlayerName(playerid, pname, sizeof(pname));
strreplace(pname, '_', ' ');
Reply
#5

Thankyou.
Reply
#6

pawn Код:
stock GetPlayerFirstName(playerid)
{
    new
        namestring[2][MAX_PLAYER_NAME],
        name[MAX_PLAYER_NAME];

    GetPlayerName(playerid,name,MAX_PLAYER_NAME);
    split(name, namestring, '_');
    return namestring[0];
}

stock GetPlayerLastName(playerid)
{
    new
        namestring[2][MAX_PLAYER_NAME],
        name[MAX_PLAYER_NAME];

    GetPlayerName(playerid,name,MAX_PLAYER_NAME);
    split(name, namestring, '_');
    return namestring[1];
}
stock split(const strsrc[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc)){
        if(strsrc[i]==delimiter || i==strlen(strsrc)){
            len = strmid(strdest[aNum], strsrc, li, i, 128);
            strdest[aNum][len] = 0;
            li = i+1;
            aNum++;
        }
        i++;
    }
    return 1;
}
USAGE: in the format() function, create two defenitions: %s %s, then match the first one with GetPlayerFirstName, and the second one with GetPlayerLastName, that way, you have removed the underscovre (_) with a space.
Reply
#7

Quote:
Originally Posted by phillip875
Посмотреть сообщение
error 033: array must be indexed (variable "-unknown-")

Shouldnt the "if( nickname[ i ] == "_" )" use strfind?
if ( nickname[ i ] == '_' )

correct
Reply
#8

http://forum.sa-mp.com/showthread.ph...93#post1845593 - Use this function to delete a sub-string from your string, like in player name. I don't know if it works to delete sub-strings with this function on player names, but try it
Reply
#9

Anyone know how to replace [ and ] since it registers as invalid symbol?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)