PDA

View Full Version : swap string


CodeBreaker
01/08/2015, 07:53 PM
hi, i need to swap all '_' to spaces, how i can do this?

i tried some like this,but I received an error error 006: must be assigned to an array
for(new i = strlen(Player_RName[playerid])-1; i != -1; i--)
{
switch(Player_RName[playerid][i])
{
case '_': Player_RName[playerid][i] = " ";
}
}

xVIP3Rx
01/08/2015, 08:07 PM
new i = strfind(Player_RName[playerid] , "_");

if(i != -1)
{
Player_RName[playerid][i] = ' ';
}

Kaliber
01/08/2015, 08:10 PM
@xVIP3Rx: That would only replace 1...

You should do it like this:

for(new i=strfind(Player_RName[playerid] , "_"); i!=-1; i=strfind(Player_RName[playerid],"_",false,i+1))
{
Player_RName[playerid][i] = ' ';
}

CodeBreaker
01/08/2015, 08:14 PM
Thank you all!

xVIP3Rx
01/08/2015, 08:22 PM
@xVIP3Rx: That would only replace 1...

You should do it like this:

for(new i=strfind(Player_RName[playerid] , "_"); i!=-1; i=strfind(Player_RName[playerid],"_",false,i+1))
{
Player_RName[playerid][i] = ' ';
}

You're right, though I don't think that loop is correct, kinda confusing me

does yours code do the same as

new i;
while( i != -1 )
{
i = strfind(Player_RName[playerid] , "_");

if(pos != -1) Player_RName[playerid][pos] = ' ';
}
?

Kaliber
01/08/2015, 08:24 PM
You're right, though I don't think that loop is correct, kinda confusing me

does yours code do the same as

new i;
while( i != -1 )
{
i = strfind(Player_RName[playerid] , "_");

if(pos != -1) Player_RName[playerid][pos] = ' ';
}
?
My code is correct and yes it do the same as your code...but is a way faster ;)

xVIP3Rx
01/08/2015, 08:47 PM
My code is correct and yes it do the same as your code...but is a way faster ;)

Right, your code is "1.71875" times faster.

@CodeBreaker: You should use his code