Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing special character from a String in Android

Tags:

java

regex

I have a String as folder/File Name. I am creating folder , file with that string. This string may or may not contain some charters which may not allow to create desired folder or file

e.g

String folder = "ArslanFolder 20/01/2013";

So I want to remove these characters with "_"

Here are characters

private static final String ReservedChars = "|\?*<\":>+[]/'"; 

What will be the regular expression for that? I know replaceAll(); but I want to create a regular expression for that.

like image 747
Arslan Anwar Avatar asked Dec 03 '22 00:12

Arslan Anwar


1 Answers

Use this code:

String folder = "ArslanFolder 20/01/2013 ? / '";
String result = folder.replaceAll("[|?*<\":>+\\[\\]/']", "_");

And the result would be:

ArslanFolder 20_01_2013 _ _ _

you didn't say that space should be replaced, so spaces are there... you could add it if it is necessary to be done.

like image 50
Kent Avatar answered Dec 19 '22 16:12

Kent