AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Replace in a String that Contains Repeated Chars (https://forums.alliedmods.net/showthread.php?t=338671)

asdian 07-20-2022 04:24

Replace in a String that Contains Repeated Chars
 
*edit
i am making a costume plugin. some costumes has name suffix of "w" for women and "m" for men. to make it simple im trying to make a code that detects what character is choosed (w/m) then random costume will bound to that chara based on gender. because the costumes are so many, i'm facing this problem. the costume is named "y19s4coswolfw" for women. it has "y19s4coswolfm" counterpart for men. if men is choosen, costume should be "y19s4coswolfm" whereas for women it should be "y19s4coswolfw". im thinking to simply just rename the suffix, which is "w" to "m" for men. the code below.

Code:

if(g_choosensex[id] == 1 /*men*/ && equal(szChoosenCos[strlen(szChoosenCos)-1], "w") /*if choosen cos is have "w" suffix*/)
{
        // replace char
        replace(szChoosenCos, charsmax(szChoosenCos), "w", "m")
                       
        new szCheck[64]
        formatex(szCheck, charsmax(szCheck), "models/costume/%s.mdl", szChoosenCos)
       
        // if doesn't exists then delete any suffix
        if(!file_exists(szCheck))
        {
                if(equal(szChoosenCos[strlen(szChoosenCos)-2], "_m")) replace(szChoosenCos, charsmax(szChoosenCos), szChoosenCos[strlen(szChoosenCos)-2], "")
                if(equal(szChoosenCos[strlen(szChoosenCos)-1], "m")) replace(szChoosenCos, charsmax(szChoosenCos), szChoosenCos[strlen(szChoosenCos)-1], "")
        }
}

but the result of renaming is
Code:

y19s4cosmolfw
which means the first 'w' from "y19s4coswolfw" is replaced instead the suffix 'w' i want.

Also i've tried :
Code:

replace(sz, charsmax(sz), sz[strlen(sz)-1], "m")
which is in my understanding will directed to the suffix but still gave same "y19s4cosmolfw" result.

How can i solve this?

+ARUKARI- 07-20-2022 05:21

Re: Replace in a String that Contains Repeated Chars
 
:roll:
Code:

sz[4] = 'm';

asdian 07-20-2022 07:19

Re: Replace in a String that Contains Repeated Chars
 
Quote:

Originally Posted by +ARUKARI- (Post 2784266)
:roll:
Code:

sz[4] = 'm';

yeah i knew it. of course i want the dynamic way. that was just an example

damage220 07-20-2022 08:39

Re: Replace in a String that Contains Repeated Chars
 
Please better describe your problem. Do you want to replace all characters that presented in the string two or more times starting from second occurrence?
abcd[a]ef[b][a]g -> abcdefg
Or specific character, or the last one?

asdian 07-20-2022 09:16

Re: Replace in a String that Contains Repeated Chars
 
Quote:

Originally Posted by damage220 (Post 2784278)
Please better describe your problem. Do you want to replace all characters that presented in the string two or more times starting from second occurrence?
abcd[a]ef[b][a]g -> abcdefg
Or specific character, or the last one?

edited the post. sorry for inconvenience..

damage220 07-20-2022 09:45

Re: Replace in a String that Contains Repeated Chars
 
Quote:

Originally Posted by asdian (Post 2784284)
edited the post. sorry for inconvenience..

It is much clearer now. So, you need only to change the last character. +ARUKARI- has already posted an answer, you need to edit the last cell in an array.
PHP Code:

szChoosenCos[strlen(szChoosenCos) - 1] = 'w'

String is an array of characters terminated with null byte. You can replace
PHP Code:

equal(szChoosenCos[strlen(szChoosenCos)-1], "w"

with
PHP Code:

if(szChoosenCos[strlen(szChoosenCos)-1] == 'w'// note single quote 

Then, if you want to delete the suffix, you should terminate the string with null byte.
PHP Code:

szChoosenCos[strlen(szChoosenCos) - 1] = '^0'

You can also apply suffix with formatex which may be better depending on your needs.
PHP Code:

formatex(szCheckcharsmax(szCheck), "models/costume/%s_w.mdl"szChoosenCos

Or even create a matrix with all the models to speed things up.
PHP Code:

new const models[][][] = {
    {
"models/costume/model1_m.mdl""models/costume/model1_w.mdl"},
    {
"models/costume/model2_m.mdl""models/costume/model2_w.mdl"},
    {
"models/costume/model3_m.mdl""models/costume/model3_w.mdl"},
};

// get first man model
models[0][0


asdian 07-20-2022 10:01

Re: Replace in a String that Contains Repeated Chars
 
Quote:

Originally Posted by damage220 (Post 2784286)
It is much clearer now. So, you need only to change the last character. +ARUKARI- has already posted an answer, you need to edit last cell in an array.
PHP Code:

szChoosenCos[strlen(szChoosenCos) - 1] = 'w'

String is an array of characters terminated with null byte. You can replace
PHP Code:

equal(szChoosenCos[strlen(szChoosenCos)-1], "w"

with
PHP Code:

if(szChoosenCos[strlen(szChoosenCos)-1] == 'w'// note single quote 

Then, if you want to delete the suffix, you should terminate the string with null byte.
PHP Code:

szChoosenCos[strlen(szChoosenCos) - 1] = '^0'

You can also apply suffix with formatex which may be better depending on your needs.
PHP Code:

formatex(szCheckcharsmax(szCheck), "models/costume/%s_w.mdl"szChoosenCos

Or even create a matrix with all the models to speed things up.
PHP Code:

new const models[][][] = {
    {
"models/costume/model1_m.mdl""models/costume/model1_w.mdl"},
    {
"models/costume/model2_m.mdl""models/costume/model2_w.mdl"},
    {
"models/costume/model3_m.mdl""models/costume/model3_w.mdl"},
};

// get first man model
models[0][0


so there's no need to use replace native ?

also if i want to delete suffix with underscore symbol is it just change
Quote:

PHP Code:

szChoosenCos[strlen(szChoosenCos) - 1] = '^0'


to
PHP Code:

szChoosenCos[strlen(szChoosenCos) - 2] = '^0'

?

damage220 07-20-2022 11:09

Re: Replace in a String that Contains Repeated Chars
 
replace is not you need according to its description.
"Given a string, replaces the first occurrence of a search string with a
replacement string."

"also if i want to delete suffix with underscore symbol is it just change to ...?"
Yes, but when doing
PHP Code:

szChoosenCos[strlen(szChoosenCos) - 2] = '^0'

make sure the string contains at least 2 characters.

Bugsy 07-20-2022 12:42

Re: Replace in a String that Contains Repeated Chars
 
PHP Code:

new szString[] = "models/costume/model1_w.mdl";
    
//Change w to m and w to m
szStringstrlenszString ) - ] = 'm';

//Delete underscore and everything after
szStringstrlenszString ) - ] = EOS


wilian159 07-20-2022 16:50

Re: Replace in a String that Contains Repeated Chars
 
this is certainly not the best way to do it, you can simply make an array with each 'kit' and save it in some variable and then retrieve it with the correct position of the costumes.

like this -> https://bit.ly/3cuYCED


All times are GMT -4. The time now is 00:19.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.