SA-MP Forums Archive
Changing one specific character in a 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: Changing one specific character in a string (/showthread.php?tid=543834)



Changing one specific character in a string - knackworst - 29.10.2014

Hi

I was wondering, how could I change a specific character in a string, without having to reformat the entire string manually?

I tried using strins and strdel in order to do this:
pawn Код:
strdel(PInfo[playerid][string], charpos, charpos + 1);
            strins(PInfo[playerid][string], "N", charpos);
Where PInfo[playerid][string] is the string that we are going to modify, charpos stands for the position of the character that we are going to change, and N stands for the new character that we are going to instert.

At first sight this seems to work, however everytime I try this, the string is properly modified but at the end of the string there's some random stuff added that has nothing to do with the string...

So for example if I were to modify the following string:
Код:
Hello
, and wanted to change it to this:
Код:
HelLo
, I get this:
Код:
HelLo Efsqaef
Does anyone know why this happens?
And is there a better way to modify a certain character in a string and keep the rest untouched?
Thanks in advance


Re: Changing one specific character in a string - VishvaJeet - 29.10.2014

User str_replace() funtion

Код:
new string[24];
format(string,24,"Ajax_Hello");

new strrep[64];
format(strrep, 64, str_replace("l","L",string));
SendClientMessage(playerid, -1, strrep);

//Output will be  Ajax_HeLLo
https://sampwiki.blast.hk/wiki/Strlib/str_replace


Re: Changing one specific character in a string - knackworst - 30.10.2014

Thank you, I didn't know str_replace existed


Re: Changing one specific character in a string - Vince - 30.10.2014

It doesn't, it's a custom function. But if you only want to replace a single character, then the only thing you need is this:
pawn Код:
string[charpos] = 'N';