Editing strreplace stock
#1

Hi,

I found this stock lying around:
pawn Код:
stock strreplace(string[], find, replace)
{
    for(new i=0; string[i]; i++)
    {
        if(string[i] == find)
        {
            string[i] = replace;
        }
    }
}
and want to know what it is I need to edit for it to allow me to replace more than one character at a time, and if you have the time, an explanation as to why or why what I'm looking to do isn't possible.

Thanks!
Reply
#2

pawn Код:
strreplace(string[], find[], replace[])
{
    new stringFound = strfind(string, find);
    if(stringFound > -1)
    {
        strdel(string, stringFound, strlen(find));
        strins(string, replace, stringFound);
    }
    return string;
}
Untested since I just wrote it up, not sure if it'll work.
Reply
#3

it is possible, and i can think of a few different ways

post an example of it in use
for example

do you want to replace one for one like replace all 'c' with 'a'
replace all 'b' with 'c'

or replace all 'c' and 'b' with 'a'
Reply
#4

Quote:
Originally Posted by SuperViper
Посмотреть сообщение
pawn Код:
strreplace(string[], find[], replace[])
{
    new stringFound = strfind(string, find);
    if(stringFound > -1)
    {
        strdel(string, stringFound, strlen(find));
        strins(string, replace, stringFound);
    }
    return string;
}
Untested since I just wrote it up, not sure if it'll work.

That hasn't quite done it, the same errors are popping up.

Quote:
Originally Posted by Jonny5
Посмотреть сообщение
it is possible, and i can think of a few different ways

post an example of it in use
for example

do you want to replace one for one like replace all 'c' with 'a'
replace all 'b' with 'c'

or replace all 'c' and 'b' with 'a'
I want to replace a '1' with 'first aid kit', '2' with 'adrenaline shot', and so on. Normally I've used this for replacing underscores with spaces.
Reply
#5

By Slice

pawn Код:
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;
}
Reply
#6

Here's a function I made a while back to do what you're asking.

pawn Код:
stock str_replace(const needle[], const replace[], haystack[], needlen = sizeof(needle), haylen = sizeof(haystack))
{
    new
        count,
        index = strfind(haystack, needle, true);
   
    while(index != -1)
    {
        strdel(haystack, index, index + (needlen - 1));
        strins(haystack, replace, index, haylen);
       
        index = strfind(haystack, needle, true);
        count++;
    }
    return count;
}
For your example:

pawn Код:
new string[] = "1 bla bla bla";

str_replace("1", "first aid kit", string);

print(string); // first aid kit bla bla bla
Hope that's what you were looking for!

Edit: Was too late, Slice's is probably more efficient.
Reply
#7

Quote:
Originally Posted by Kar
Посмотреть сообщение
By Slice

pawn Код:
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;
}
I received the same errors as the other two.

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Here's a function I made a while back to do what you're asking.

pawn Код:
stock str_replace(needle[], replace[], haystack[])
{
    new index = strfind(haystack, needle, true);
   
    while(index != -1)
    {
        strdel(haystack, index, index + strlen(needle));
        strins(haystack, replace, index, strlen(haystack));
       
        index = strfind(haystack, needle, true);
    }
}
For your example:

pawn Код:
new string[] = "1 bla bla bla";

str_replace("1", "first aid kit", string);

print(string); // first aid kit bla bla bla
Hope that's what you were looking for!

Edit: Was too late, Slice's is probably more efficient.
Yours worked! Thanks to the rest of you too.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)