[Question] strdel(string, strfind...) ?
#1

pawn Код:
stock rpname(playerid)
{
    new cname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, cname, sizeof(cname));
    strdel(cname, strfind(cname, "_", true) > 0 && strfind(cname, "_", true) < MAX_PLAYER_NAME, MAX_PLAYER_NAME);
    return cname;
}
So I made up this piece of shit and when I use the function like:
pawn Код:
printf("%s", rpname(playerid));
It only prints the first character of the players name..
I was wondering if what I'm trying to do is even possbible?
And if it is, what am I doing wrong?
Reply
#2

Why are you trying to force an if test inside strdel?

pawn Код:
stock rpname(playerid)
{
    new cname[MAX_PLAYER_NAME], charpos;
    GetPlayerName(playerid, cname, sizeof(cname));
    charpos = strfind(cname, "_"); //get the position of the '_' if it exists inside the name.
    if(charpos != -1) //if charpos is -1, that means that '_' was not found, else it's the position of '_'
        strdel(cname, charpos, charpos); //delete the '_'
    return cname; //return the final result
}
Reply
#3

I don't know actually.. I was just trying something out..

correction though:
pawn Код:
strdel(cname, charpos, charpos+1);
Do you know how to replace the "_" ?
Isn't there a string replace function ?


Nevermind, I figured this out:
pawn Код:
stock rpname(playerid)
{
    new cname[MAX_PLAYER_NAME], underscore;
    GetPlayerName(playerid, cname, sizeof(cname));
    underscore = strfind(cname, "_");
    if(underscore != -1)
    {
        strdel(cname, underscore, underscore+1);
        strins(cname, " ", underscore, MAX_PLAYER_NAME);
    }
    return cname;
}
Reply
#4

You can also use this function
pawn Код:
stock strreplace(string[], find, replace)
{
    for(new i=0; string[i]; i++)
    {
        if(string[i] == find)
        {
            string[i] = replace;
        }
    }
}
pawn Код:
stock rpname(playerid)
{
    new cname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, cname, sizeof(cname));
    strreplace(cname, '_', ' ');
    return cname;
}
Reply
#5

This also works, assuming they have a "_" in their name.

pawn Код:
cname[strfind(cname, "_")] = ' ';
Reply
#6

Thank you everyone!
Reply
#7

Quote:
Originally Posted by ******
Посмотреть сообщение
And if they don't it crashes them...
Wouldn't it crash the script itself rather than the player?
Reply
#8

Quote:
Originally Posted by ******
Посмотреть сообщение
And if they don't it crashes them...
Yes. Hence I had the little disclaimer. More on this in the tutorial denoted to this topic:

https://sampforum.blast.hk/showthread.php?tid=218694
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)