How to replace string? - 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: How to replace string? (
/showthread.php?tid=561125)
How to replace string? -
basicllsw - 01.02.2015
Hi all i have question about how to replace string.
when i type "Hello @1 How are you" i want to replace @1 to name of playerid1
Thanks
ps. sorry if my english is bad
Re: How to replace string? -
BroZeus - 01.02.2015
Download strlib inc from this topic
https://sampforum.blast.hk/showthread.php?tid=85697
After including that include use the function mentioned here -
https://sampwiki.blast.hk/wiki/Strlib/str_replace
Re: How to replace string? -
basicllsw - 01.02.2015
Quote:
Originally Posted by BroZeus
|
thank i try with other strlib include with command
Код:
cmd:test(pid,params[])
{
new text[128],output[128][10],string[128],count;
if(!sscanf(params,"s[128]",text))
{
count = strexplode(output,text,"@");
for (new i = 0; i < count; i++)
{
format(string,128,"output[%i] = %s",i,output[i]);
SendClientMessage(pid,-1,string);
if(IsNumeric(output[i]))
{
new id = strval(output[i]);
new name[128];
GetPlayerName(id, name, sizeof(name));
format(string,sizeof(string),"outputEncode[%i] = %s idconvert=%i",i,name,id);
SendClientMessage(pid,-1,string);
}
}
}
return 1;
}
when i type /test Hello @1@How Are You. it can explode to output[]
output[1] = Hello output[2] 1 output[3] = How Are You but when i convert to userid it show blank string like
Код:
outputEncode[2]= idconvert=1
how to fix it
Re: How to replace string? -
Jefff - 01.02.2015
pawn Код:
str_replace(const text[])
{
new str[145];
strcat(str,text);
new pos = strfind(str,"@",true);
if(pos != -1)
{
new ids[5];
while((pos = strfind(str,"@",true,pos)) != -1)
{
new playerid;
if(!sscanf(str[pos+1],"i",playerid))
if(0 <= playerid < MAX_PLAYERS && IsPlayerConnected(playerid))
{
new name[MAX_PLAYER_NAME + 1];
GetPlayerName(playerid,name,sizeof(name));
valstr(ids,playerid);
strdel(str,pos,pos+strlen(ids)+1);
strins(str,name,pos);
pos += 3;
}
pos += 1;
}
}
return str;
}
CMD:test(playerid, params[])
{
if(!isnull(params))
SendClientMessageToAll(-1,str_replace(params));
return 1;
}
Re: How to replace string? -
basicllsw - 02.02.2015
Quote:
Originally Posted by Jefff
pawn Код:
str_replace(const text[]) { new str[145]; strcat(str,text); new pos = strfind(str,"@",true); if(pos != -1) { new ids[5]; while((pos = strfind(str,"@",true,pos)) != -1) { new playerid; if(!sscanf(str[pos+1],"i",playerid)) if(0 <= playerid < MAX_PLAYERS && IsPlayerConnected(playerid)) { new name[MAX_PLAYER_NAME + 1]; GetPlayerName(playerid,name,sizeof(name)); valstr(ids,playerid); strdel(str,pos,pos+strlen(ids)+1); strins(str,name,pos); pos += 3; }
pos += 1; } } return str; }
CMD:test(playerid, params[]) {
if(!isnull(params)) SendClientMessageToAll(-1,str_replace(params));
return 1; }
|
thank for guide but it's send original input again. it not convert @ to playername ex input = "Hello@1How" output = "Hello@1How"
Re: How to replace string? -
Jefff - 02.02.2015
because its wrong usage? you wanted "Hello @1 How" its better than "HellobasicllswHow"