替换fileName中的非法字符 [英] Replacing illegal character in fileName
本文介绍了替换fileName中的非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,我有一个文件名字符串.在那里,我想用'_'代替所有非法字符,而不是 az
, 0-9
,-
,.
和 _
In Java, I've a File-Name-String. There I want to replace all illegal Characters with '_', but not a-z
, 0-9
, -
,.
and _
我尝试了以下代码:但这没有用!
I tried following code: But this did not worked!
myString = myString.replaceAll("[\\W][^\\.][^-][^_]", "_");
推荐答案
您需要替换除 [a-zA-Z0-9 .-]
以外的所有内容.括号内的 ^
表示"NOT".
You need to replace everything but [a-zA-Z0-9.-]
.
The ^
within the brackets stands for "NOT".
myString = myString.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
这篇关于替换fileName中的非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文