SA-MP Forums Archive
Replace a word with another. - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Replace a word with another. (/showthread.php?tid=221631)



Replace a word with another. - Zh3r0 - 05.02.2011

I tried different types of code...couldn't make it work...

Any ideas ? I tried strreplace...not working...


Re: Replace a word with another. - xRyder - 05.02.2011

Not sure if this will help, but you can use strdel and strins.
Example: (I found it somewhere around here)
pawn Code:
new s1[128] = "I'm eating cakes";
new s2[] = "shitting";

strdel(s1, 4, 10);
strins(s1, s2, 4);
   
print(s1);



Re: Replace a word with another. - Zh3r0 - 05.02.2011

Quote:
Originally Posted by xRyder
View Post
Not sure if this will help, but you can use strdel and strins.
Example: (I found it somewhere around here)
pawn Code:
new s1[128] = "I'm eating cakes";
new s2[] = "shitting";

strdel(s1, 4, 10);
strins(s1, s2, 4);
   
print(s1);
I actually tried with strdel and strins.

The thing is that i must use a loop and search on the text for the word. And replace it.
When i tried with strdel and strins it either crashed my server, or the text was weird.


Re: Replace a word with another. - Mauzen - 05.02.2011

Use strfind to get the position of the word you want to replace. THen, when using strdel and strins be sure you are not mixing up the indexes and lengths.


Re: Replace a word with another. - Hiddos - 05.02.2011

Use strfind to get the location, then strdel to remove text the word out of a text (by the response of strfind + length of the word), then strins to input the new word.


Re: Replace a word with another. - [03]Garsino - 05.02.2011

pawn Code:
strreplace(some string, "Hello", "Hola");
pawn Code:
stock strreplace2(string[], const find[], const replace[], bool:ignorecase=false, count=cellmax, maxlength=sizeof(string))
{
    if(isnull(find) || isnull(string)) return 0;
    new matches;
    for(new idx, flen = strlen(find), rlen = strlen(replace), pos = strfind(string, find, ignorecase); pos != -1 && idx < maxlength && matches < count; pos = strfind(string, find, ignorecase, idx)) {
        strdel(string, pos, pos + flen);
        if(rlen) strins(string, replace, pos, maxlength);
        idx = pos + rlen;
        matches++;
    }
    return matches;
}