SA-MP Forums Archive
str_replace, how to change char. - 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: str_replace, how to change char. (/showthread.php?tid=638573)



str_replace, how to change char. - Zamek - 02.08.2017

Hello! Sorry for my bad english, i'm polish.

I have a problem with str_replace.
Quote:

new sSearch[] = {"ą", "ć"};
new sReplace[] = {"a", "c"};
new sSubject[] = "Asią byla ćasna.";

for(new i; i < sizeof(sSearch); i++)
{
str_replace(sSearch[i], sReplace[i], sSubject);
}

printf("%s", sSubject);

Original text: Asią byla ćasna
Change text: Asia byla casna

How to make??


Re: str_replace, how to change char. - Zamek - 03.08.2017

Please help me!


Re: str_replace, how to change char. - Hansrutger - 03.08.2017

Iterate through the whole sSubject and find the ones you need to change instead, that's how I would have done it at least. I don't know where you can get strlib from, the wiki page is being nab and it doesn't tell. But I'll show you how you can do it without str_replace.

Код:
for (new i = 0; i < sizeof(sSubject); i++)
{
    if (sSubject[i] == 'ą')
    {
         sSubject[i] = 'a';
    }
    else if (sSubject[i] == 'ć')
    {
         sSubject[i] = 'c';
    }
}
Replacing whole words will force you to use something like strcmp or similar. Again maybe str_replace works, I don't know because I've never seen where you could download that library from (neither have I given any effort in finding it other than trying from the wiki).


Re: str_replace, how to change char. - OsteeN - 03.08.2017

Replace:
PHP код:
new i
.. with:
PHP код:
new 0
.. and see if it works.


Re: str_replace, how to change char. - Hansrutger - 03.08.2017

Quote:
Originally Posted by OsteeN
Посмотреть сообщение
Replace:
PHP код:
new i
.. with:
PHP код:
new 0
.. and see if it works.
Obviously there can only be one king of the north here!

If you just declare things without giving them any value, they automatically get a default value, for integers it's 0 and for booleans it's false, which also stands for 0.


Re: str_replace, how to change char. - Zamek - 03.08.2017

Quote:
Originally Posted by Hansrutger
Посмотреть сообщение
Iterate through the whole sSubject and find the ones you need to change instead, that's how I would have done it at least. I don't know where you can get strlib from, the wiki page is being nab and it doesn't tell. But I'll show you how you can do it without str_replace.

Код:
for (new i = 0; i < sizeof(sSubject); i++)
{
    if (sSubject[i] == 'ą')
    {
         sSubject[i] = 'a';
    }
    else if (sSubject[i] == 'ć')
    {
         sSubject[i] = 'c';
    }
}
Replacing whole words will force you to use something like strcmp or similar. Again maybe str_replace works, I don't know because I've never seen where you could download that library from (neither have I given any effort in finding it other than trying from the wiki).
Thanks for help!


Re: str_replace, how to change char. - OsteeN - 03.08.2017

PHP код:
    new sFind[][] = {"ą""ć"};
    new 
sReplace[][] = {"a""c"};
    new 
sSubject[500] = "Asią byla ćasna.";
    
printf("Old string: %s"sSubject);
    for(new 
0sizeof(sFind); i++)
    {
        
sSubject str_replace(sFind[i], sReplace[i], sSubject);
    }
    
printf("New string: %s"sSubject); 
Here you go, as you wanted it. Obviously play around with the size of the `sSubject` string.

Regarding where to find strlib: https://sampforum.blast.hk/showthread.php?tid=85697

To add any other replacements, simply add it to the arrays.