Changing one specific character in a string
#1

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

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

Thank you, I didn't know str_replace existed
Reply
#4

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';
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)