• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

Replace multiple characters in a string

Hey having trouble figuring how to replace multiple sets of characters in a string at once.

I have a file save and load system where the player picks the save file name but to do that i need to make sure they don't do anything silly like putting @,£,$,/,| etc in the file name so how would i go through and replace all them characters or would i have to do each one manually with string_replace_all()? Thanks
 

GMWolf

aka fel666
You could have a string or array of the chars you want to replace and use a loop with string replace all.
 
You could have a string or array of the chars you want to replace and use a loop with string replace all.
I was afraid of that but it would be easier than going through the one by one and doing them all manually. Thanks alot i'll give it a try
 

TsukaYuriko

Q&A Spawn Camper
Forum Staff
Moderator
Alternative idea: Instead of filtering characters out based on a blacklist, filter them with a whitelist. There are way less allowed characters than non-allowed ones, so how about checking character by character if the given character is allowed, adding it to the final file name if it is, and omitting it if it isn't? If you define a string of all allowed characters, you could check a single character against all of the allowed characters by using something like string_pos or string_count.
 
Alternative idea: Instead of filtering characters out based on a blacklist, filter them with a whitelist. There are way less allowed characters than non-allowed ones, so how about checking character by character if the given character is allowed, adding it to the final file name if it is, and omitting it if it isn't? If you define a string of all allowed characters, you could check a single character against all of the allowed characters by using something like string_pos or string_count.
Thanks for the suggestions but i used the previous one and it works fine. I'll post the code below incase anyone wants it (nothing special):
Code:
///file_getname(string)
var str = string(argument[0]);

var array = ["@","\\","/","!","\"","£","$","%","^","&","*","(",")","_","+","-","=","[","]","{","}",";","'","#",":","@","~",",",".","/","<",">","?","|","`","¬"];

for(i=0;i<array_length_1d(array);i+=1)
{
    str = string_replace_all(str,array[i],"");
}

return str;
 

Yal

GMC Memer
GMC Elder
With Unicode there's THOUSANDS of characters that may or may not cause problems, some of which aren't even defined yet, so there's advantages to whitelists...
 
With Unicode there's THOUSANDS of characters that may or may not cause problems, some of which aren't even defined yet, so there's advantages to whitelists...
Thanks for the comment. I also thought about people in other countries with different character sets, their OS would allow it but if it's not on the whitelist they would have to use english characters (I think that's how it works)

EDIT: One way around it would just to be to display an error message if file_text_open_write() didn't work and tell them to change the name which is probably what i'm going to go with aswell as the basic character banlist
 
Top