SA-MP Forums Archive
Best way of replacing 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: Best way of replacing a string? (/showthread.php?tid=396978)



Best way of replacing a string? - robanswe - 02.12.2012

Hello, What is the best way to replace a string?

Example: format(test[playerid], sizeof(test[playerid]), "%s","replaceing");

I have heard that strcat is better but that function only seems to join together two strings... I don't wont to do that I like to replace the whole content of test[playerid].


Re: Best way of replacing a string? - maramizo - 02.12.2012

This does replace the test[playerid] string with "replaceing".
If it does not do it successfully, then either the ShowClientMessage OR the format line does NOT get read by the compiler.


Re: Best way of replacing a string? - Intoxicated - 02.12.2012

strpack(string, newstring);

Example: strpack(string, "Hi, how are you?");

Source: sa-mp wiki.


Re: Best way of replacing a string? - robanswe - 03.12.2012

Thank you!


Re: Best way of replacing a string? - Faisal_khan - 03.12.2012

What about this by Slice:
pawn Code:
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
    // No need to do anything if the limit is 0.
    if (limit == 0)
        return 0;
   
    new
             sublen = strlen(search),
             replen = strlen(replacement),
        bool:packed = ispacked(string),
             maxlen = maxlength,
             len = strlen(string),
             count = 0
    ;
   
   
    // "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
    // Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
    if (packed)
        maxlen *= 4;
   
    // If the length of the substring is 0, we have nothing to look for..
    if (!sublen)
        return 0;
   
    // In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
    while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
        // Delete the string we found
        strdel(string, pos, pos + sublen);
       
        len -= sublen;
       
        // If there's anything to put as replacement, insert it. Make sure there's enough room first.
        if (replen && len + replen < maxlen) {
            strins(string, replacement, pos, maxlength);
           
            pos += replen;
            len += replen;
        }
       
        // Is there a limit of number of replacements, if so, did we break it?
        if (limit != -1 && ++count >= limit)
            break;
    }
   
    return count;
}
Usage:
pawn Code:
new string[128] = "Hello world";

strreplace(string, "world", "earth"); // string = "Hello earth"
strreplace(string, "HELLO", "Hola"); // string = "Hello earth" (no match for "HELLO")
strreplace(string, "HELLO", "Hola", .ignorecase = true); // string = "Hola earth"