Replacing illegal character in fileName

后端 未结 7 1962
情书的邮戳
情书的邮戳 2020-12-24 10:39

In Java, I\'ve a File-Name-String. There I want to replace all illegal Characters with \'_\', but not a-z, 0-9, -,. and <

7条回答
  •  情深已故
    2020-12-24 11:09

    Even simpler

    myString = myString.replaceAll("[^\\w.-]", "_");
    

    Predefined Character Classes:

    • \w A word character: [a-zA-Z_0-9]

提交回复
热议问题