I tried using this but didn't work-
return value.replaceAll("/[^A-Za-z0-9 ]/", "");
I tried using this but didn't work-
return value.replaceAll("/[^A-Za-z0-9 ]/", "");
return value.replaceAll("[^A-Za-z0-9 ]", "");
This will leave spaces intact. I assume that's what you want. Otherwise, remove the space from the regex.
Try
return value.replaceAll("[^A-Za-z0-9]", "");
or
return value.replaceAll("[\\W]|_", "");
Java's regular expressions don't require you to put a forward-slash (/
) or any other delimiter around the regex, as opposed to other languages like Perl, for example.
I made this method for creating filenames:
public static String safeChar(String input)
{
char[] allowed = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".toCharArray();
char[] charArray = input.toString().toCharArray();
StringBuilder result = new StringBuilder();
for (char c : charArray)
{
for (char a : allowed)
{
if(c==a) result.append(a);
}
}
return result.toString();
}