Not removing underscore
#1

Код:
GiveNameSpace(str[])
{
    new strl;
    strl=strlen(str);
    while(strl--) {
    if(str[strl]=='_')	str[strl]=' ';
    }
    return 0;
}
Код:
stock GetPlayerNameEx(playerid,ENameType:type) {
	new name[MAX_PLAYER_NAME+1];
	//SetPVarString(playerid,"CharUsername",id_string);
	if(IsPlayerConnected(playerid)) {
		switch(type) {
			case ENameType_AccountName: {
				GetPVarString(playerid,"AccountName",name,MAX_PLAYER_NAME);
			}
			case ENameType_RPName: {
				new maskid = GetPVarInt(playerid, "MaskID");
				if(GetPVarInt(playerid, "MaskOn") != 1) {				
					GetPVarString(playerid,"CharUsername",name,MAX_PLAYER_NAME);
				} else {
					format(name, sizeof(name), "Stranger_%d",maskid);
				}
				GiveNameSpace(name);
			}
			case ENameType_CharName: {
				GetPVarString(playerid,"CharUsername",name,MAX_PLAYER_NAME);
			}
			case ENameType_RPName_NoMask: {
				GetPVarString(playerid,"CharUsername",name,MAX_PLAYER_NAME);
				GiveNameSpace(name);
			}
			case ENameType_Phone_Name: {
				GetPVarString(playerid,"CharUsername",name,MAX_PLAYER_NAME);
				if(GetPVarInt(playerid, "PhoneStatus") == 1) {
					strmid(name,"Anonymous",0,9,MAX_PLAYER_NAME);
				}
				if(GetPVarInt(playerid, "PhoneStatus") == 3) {
					strmid(name,"Payphone",0,9,MAX_PLAYER_NAME);
				}
				GiveNameSpace(name);			
			}
		}
		if(strlen(name) < 1) { //null name(either not logged in or a bug happend to them
			GetPlayerName(playerid, name, sizeof(name));
		}
	} else {
		GetPlayerName(playerid, name, sizeof(name));
	}
	return name;
}
Reply
#2

pawn Код:
// ** INCLUDES

#include <a_samp>

// ** MAIN

main()
{
    print("Loaded \"replace_string.amx\".");

    new source[128] = "CaNDy_Boy_Mr Sr_Hey";
    ReplaceString(source, "_", " ");

    printf("%s", source);
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** FUNCTIONS

stock ReplaceString(string[], const search[], const replace[], size_of = sizeof(string))
{
    new search_length = strlen(search), temp, last_pos;
    for(;;)
    {
        temp = strfind(string, search, false, last_pos);

        if(temp != -1)
        {
            strdel(string, temp, (temp + search_length));
            strins(string, replace, temp, size_of);

            last_pos = (temp + search_length);
        }
        else
        {
            break;
        }
    }
    return 1;
}
Reply
#3

i believe
Quote:

while(strl--)

is an infinite loop

try

Quote:

stock GiveNameSpace(str[])
{
new strl;
for(strl=strlen(str); strl > 0; strl--)
{
if(str[strl]=='_')
{
str[strl]=' ';
}
}
return 0;
}

^^^removes every underscore found in the string

and sorry i must have completly missed ur post, its like 2 am where i am
Reply
#4

Quote:
Originally Posted by chrism11
Посмотреть сообщение
changed my mind looking into it
I gave him a "universal" function, I wonder what you'll come up with.

The function replaces all matches found (e.g. The_Spookie_Man would become The Spookie Man).
Reply
#5

String functions are much slower. That universal function is fine if you want to replace actual words, but if you only want to replace some characters then a plain loop ought to be much faster.
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
String functions are much slower. That universal function is fine if you want to replace actual words, but if you only want to replace some characters then a plain loop ought to be much faster.
Yeah, by about 47 milliseconds (50,000 iterations => 105 milliseconds / 152 milliseconds).

pawn Код:
stock ReplaceUnderscoreWithSpace(source[])
{
    for(new i = 0, j = strlen(source); i < j; i ++)
    {
        if(source[i] == '_')
        {
            source[i] = ' ';
        }
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)