How to replace string?
#1

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
Reply
#2

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
Reply
#3

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
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
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
Reply
#4

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;
}
Reply
#5

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"
Reply
#6

because its wrong usage? you wanted "Hello @1 How" its better than "HellobasicllswHow"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)