Raised This Month: $344 Target: $400
 86%   Almost there!

Solved Replace in a String that Contains Repeated Chars


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
asdian
Junior Member
Join Date: Sep 2016
Location: Indonesia
Old 07-20-2022 , 04:24   Replace in a String that Contains Repeated Chars
Reply With Quote #1

*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?

Last edited by asdian; 08-14-2022 at 08:58. Reason: to make it clearer
asdian is offline
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 07-20-2022 , 05:21   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #2

Code:
sz[4] = 'm';
__________________
GitHub
SteamWishlist

六四天安門事件
+ARUKARI- is offline
asdian
Junior Member
Join Date: Sep 2016
Location: Indonesia
Old 07-20-2022 , 07:19   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #3

Quote:
Originally Posted by +ARUKARI- View Post
Code:
sz[4] = 'm';
yeah i knew it. of course i want the dynamic way. that was just an example

Last edited by asdian; 07-20-2022 at 07:23.
asdian is offline
damage220
Junior Member
Join Date: Jul 2022
Location: Ukraine
Old 07-20-2022 , 08:39   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #4

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?

Last edited by damage220; 07-20-2022 at 08:59.
damage220 is offline
asdian
Junior Member
Join Date: Sep 2016
Location: Indonesia
Old 07-20-2022 , 09:16   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #5

Quote:
Originally Posted by damage220 View Post
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..
asdian is offline
damage220
Junior Member
Join Date: Jul 2022
Location: Ukraine
Old 07-20-2022 , 09:45   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #6

Quote:
Originally Posted by asdian View Post
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

Last edited by damage220; 07-20-2022 at 09:59.
damage220 is offline
asdian
Junior Member
Join Date: Sep 2016
Location: Indonesia
Old 07-20-2022 , 10:01   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #7

Quote:
Originally Posted by damage220 View Post
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'
?

Last edited by asdian; 07-20-2022 at 10:10. Reason: forgot question
asdian is offline
damage220
Junior Member
Join Date: Jul 2022
Location: Ukraine
Old 07-20-2022 , 11:09   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #8

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.
damage220 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-20-2022 , 12:42   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #9

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
__________________
Bugsy is offline
wilian159
Member
Join Date: Dec 2013
Old 07-20-2022 , 16:50   Re: Replace in a String that Contains Repeated Chars
Reply With Quote #10

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
__________________
Hmm, nice....... www.addonscs.com
wilian159 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:53.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode