str_replace, how to change char.
#1

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??
Reply
#2

Please help me!
Reply
#3

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

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

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

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!
Reply
#7

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


Forum Jump:


Users browsing this thread: 1 Guest(s)