views:

52

answers:

1

I have an uploader in my web page, but some people upload files named like "compañia 15% *09.jpg" and i have problems when the filenames are like that one.

I would like to found a class that returns for that example something like this: "compania1509.jpg".

+3  A: 

In other words, you'd like to get rid of all characters outside the printable ASCII range? You can use String#replaceAll() with a pattern of [^\x20-\x7e] for this.

name = name.replaceAll("[^\\x20-\\x7e]", "");

If you want to get rid of spaces as well, then start with \x21 instead. You can even restrict it to Word-characters only. Use \W to indicate "any non-word" character. The name will then match only alphanumericals and the underscore.

name = name.replaceAll("\\W", "");
BalusC
That's one way to solve it, but it's overkill for many OSes. There's no reason a filename can't contain `ñ` on most OSes, for instance.
T.J. Crowder
@T.J: that would then be more a question/answer for Superuser :)
BalusC