How to remove invalid characters from a String , so that it can be used as a file name ?
the invalid characters include ("\\/:*?\"<>|")
.
You can simply use C# inbuilt function " Path. GetInvalidFileNameChars() " to check if there is invalid character in file name and remove it. var InvalidCharacters= Path. GetInvalidFileNameChars(); string GetInvalidCharactersRemovedString= new string(fileName .
Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters. Characters that are NOT supported include, but are not limited to: @ $ % & \ / : * ? " ' < > | ~ ` # ^ + = { } [ ] ; !
You can try this,
String fileName = "\\/:*AAAAA?\"<>|3*7.pdf"; String invalidCharRemoved = fileName.replaceAll("[\\\\/:*?\"<>|]", ""); System.out.println(invalidCharRemoved);
OUTPUT
AAAAA37.pdf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With