string with invalid characters

This is a pretty straight-forward question. Within a program of mine, I have a string that's going to be used as a filename, but it might have some invalid characters in it that wouldn't be valid in a filename. If there are any invalid characters, I want to get rid of them and essentially squeeze the remaining characters together to form a valid filename.

I can't seem to find any string function that might help to assist in making this a more efficient than simply checking the string char by char, moving the valid chars into another string while ignoring the invalid ones, creating a valid filename.

Just thought I'd ask and see what kind of input I would receive. Thank you.

Try the tr command:

new_name=`echo "$filename" | tr -cd [a-zA-Z]`

Regards

IF you mean C, then yes, you use a char pointer to step thru the source string, copying only good characters to the destination string, using ctype.h macros maybe like:
isascii(),
isspace(),
iscntrl()

to check the 'goodness' of the character. The definition of what is good is partly up to you - some chars like * are valid in filenames but can cause hell to break loose when you actually use them. Otherwise use tr like Franklin says.

Thanks for the feedback, folks. Yes, I tried the 'tr' string and that looks like it will work just fine. One last question. The sample that Franklin gave indicated the characters that are legal. If I wanted to use the same form of his example, except indicating the characters that aren't legal, what would be the syntax? Putting a '!' in front of the list of characters, like 'tr -cd ![*#$%]', something like that? Thanks again.

echo filename_string | tr -d '[a-zA-Z]'