[Question] strdel(string, strfind...) ? -
Lorrden - 09.02.2011
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?
Re: [Question] strdel(string, strfind...) ? -
Krx17 - 09.02.2011
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
}
Re: [Question] strdel(string, strfind...) ? -
Lorrden - 09.02.2011
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;
}
Re: [Question] strdel(string, strfind...) ? -
MadeMan - 09.02.2011
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;
}
Re: [Question] strdel(string, strfind...) ? -
randomkid88 - 09.02.2011
This also works, assuming they have a "_" in their name.
pawn Код:
cname[strfind(cname, "_")] = ' ';
Re: [Question] strdel(string, strfind...) ? -
Lorrden - 09.02.2011
Thank you everyone!
Re: [Question] strdel(string, strfind...) ? -
Krx17 - 10.02.2011
Quote:
Originally Posted by ******
And if they don't it crashes them...
|
Wouldn't it crash the script itself rather than the player?
Re: [Question] strdel(string, strfind...) ? -
randomkid88 - 10.02.2011
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