Delete underscore from string
#1

Hello,

i try to delete an underscore (_) from a string, i try to do it in two differents way, but it does'nt work:

1st way:
pawn Код:
stock RPName(name[])
{
        new NameLenght = strlen(name);
        new value = strfind(name, "_", true);
        strdel(name, value, value+1);
        strins(name, " ", value, NameLenght);
        return name;
}
It return |dњfirstname lastname, so it work but i don't understand why there are |dњ before the name, i try to delete the three first caracter but it does'nt work.

The second way:
pawn Код:
stock Underscore(name[])
{
    for(new i=0; i<strlen(name); i++)
    {
        if (name[i] == "_") //
        {
            name[i] = " ";
            return name;
        }
    }
    return 1;
}
but it show some errors
Quote:

C:\Users\PC-HP\Desktop\pawno\gamemodes\test.pwn(1297) : error 033: array must be indexed (variable "-unknown-")
C:\Users\PC-HP\Desktop\pawno\gamemodes\test.pwn(1299) : error 006: must be assigned to an array
C:\Users\PC-HP\Desktop\pawno\gamemodes\test.pwn(1303) : error 079: inconsistent return types (array & non-array)

Reply
#2

pawn Код:
stock Underscore(name[])
{
    for(new i = 0, j = strlen(name); i != name; i++)
    {
        if (name[i] == '_') //
        {
            name[i] = ' ';
            break;
        }
    }
    return name;
}
If you delete the break, then all _ will be replaced, otherwise only first one. Also "_" is not equal to '_'. "_" is in fact string, array of 2 elements '_', '\0', and you need only character, so use single quotes
Reply
#3

Hi and thanks. Unfortunately, same problem, a |doe before the name :/
Reply
#4

pawn Код:
for(new i = 0, j = strlen(name); i != j; i++)
Reply
#5

pawn Код:
stock RemoveBracket(playerid)
{
    new szName[MAX_PLAYER_NAME];
    GetPlayerName(playerid, szName, sizeof(szName));
    for(new i = 0; i < MAX_PLAYER_NAME; i++)
    {
        if(szName[i] == '_') szName[i] = ' ';
    }
    return szName;
}
Usage:

pawn Код:
new SzString[128];
format(SzString, sizeof(SzString), "%s", RemoveBracket(playerid));
And there you have it
Reply
#6

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
for(new i = 0, j = strlen(name); i != j; i++)
pawn Код:
for(new i = 0; i < strlen(name); i++)
Its the same no ?

@Alexis1999: I want to delete underscore from a string.
Reply
#7

Quote:
Originally Posted by Garwan50
Посмотреть сообщение
pawn Код:
for(new i = 0; i < strlen(name); i++)
Its the same no ?

@Alexis1999: I want to delete underscore from a string.
Jefff's loop is somewhat faster.
Reply
#8

Ok, but it does'nt work :/
Reply
#9

Try this one, I think will help you
pawn Код:
GiveNameSpace(str[])
{
    new strl;
    strl=strlen(str);
    while(strl--) {
    if(str[strl]=='_')  str[strl]=' ';
    }
    return 0;
}
For example:
pawn Код:
new myname[MAX_PLAYER_NAME];
GetPlayerName(playerid, myname, sizeof(myname));
GiveNameSpace(myname);
new string[128];
format(string, sizeof(string), "%s", myname(playerid));
Reply
#10

Yes but i want to know what's wrong with my code ^^, thanks anyway
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)