Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing invalid characters (("\\/:*?\"<>|") ) from a string to use it as a FileName

Tags:

How to remove invalid characters from a String , so that it can be used as a file name ?
the invalid characters include ("\\/:*?\"<>|").

like image 262
curious Avatar asked Jul 22 '15 13:07

curious


People also ask

How do you remove illegal characters from path and fileName?

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 .

What characters can be used in a 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: @ $ % & \ / : * ? " ' < > | ~ ` # ^ + = { } [ ] ; !


1 Answers

You can try this,

String fileName = "\\/:*AAAAA?\"<>|3*7.pdf"; String invalidCharRemoved = fileName.replaceAll("[\\\\/:*?\"<>|]", ""); System.out.println(invalidCharRemoved); 

OUTPUT

AAAAA37.pdf 
like image 133
akash Avatar answered Sep 30 '22 18:09

akash