Does anyone know some Java class that return a ope

2019-07-31 10:38发布

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

1条回答
迷人小祖宗
2楼-- · 2019-07-31 11:02

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", "");
查看更多
登录 后发表回答