Replace a word with another.
#1

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

Any ideas ? I tried strreplace...not working...
Reply
#2

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

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.
Reply
#4

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.
Reply
#5

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.
Reply
#6

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


Forum Jump:


Users browsing this thread: 1 Guest(s)