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".

Nacho
  • 11
  • 1

1 Answers1

4

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555