Raised This Month: $51 Target: $400
 12% 

Replace a string's character inside a loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
IceCucumber
Member
Join Date: Dec 2011
Old 08-07-2014 , 12:53   Replace a string's character inside a loop
Reply With Quote #1

I'm attempting to remove all non-alphanumeric characters from a string to later check it against some phrases. If someone could help me with this I'd appreciate it.

PHP Code:
TrimMessage(String:message[256]) // Trim all non-alphanumeric characters from a message
for (new 0sizeof(message); i++)
{      
    if (!
IsCharAlpha(message[i]) || (!IsCharNumeric(message[i])))
    {
        
// remove this particular character from the string. Format? strcopy? : /
    
}


Last edited by IceCucumber; 08-07-2014 at 15:10. Reason: clarified
IceCucumber is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-07-2014 , 14:47   Re: Replace a string's character inside a loop
Reply With Quote #2

You need && not ||
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
IceCucumber
Member
Join Date: Dec 2011
Old 08-07-2014 , 15:01   Re: Replace a string's character inside a loop
Reply With Quote #3

Right. How would I go about editing (removing) the character numbered i from the string?
Code:
strcopy(message[i], sizeof(message), "");
Didn't seem to work.

Last edited by IceCucumber; 08-07-2014 at 15:02.
IceCucumber is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 08-07-2014 , 15:15   Re: Replace a string's character inside a loop
Reply With Quote #4

The solution with best performance is to build a new string and copy every character you want. This requires a for loop and two counter variables. One for the source string and one for the target string.

Not tested:
PHP Code:
new String:cleanedMessage[sizeof(message)+1];
new 
pos_cleanedMessage 0;
for (new 
0sizeof(message); i++) {
    if (
IsCharAlpha(message[i]) || IsCharNumeric(message[i])) {
        
cleanedMessage[pos_cleanedMessage++] = message[i];
    }
}

// Terminate the string with 0
cleanedMessage[pos_cleanedMessage] = '\0'
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
IceCucumber
Member
Join Date: Dec 2011
Old 08-07-2014 , 15:26   Re: Replace a string's character inside a loop
Reply With Quote #5

Thanks, both of you. This seems to be working fine.
IceCucumber is offline
Reply



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 17:48.


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