java - How to filter string for unwanted characters using regex? -
basically , wondering if there handy class or method filter string unwanted characters. output of method should 'cleaned' string. ie:
string dirtystring = "this contains spaces not allowed" string result = cleaner.getcleanedstring(dirtystring);
expecting result be:
"thiscontainsspaceswhicharenotallowed"
a better example:
string reallydirty = " this*is#a*&very_dirty&string" string result = cleaner.getcleanedstring(dirtystring);
i expect result be:
"thisisaverydirtystring"
because, let cleaner know ' ', '*', '#', '&' , '_' dirty characters. can solve using white/black list array of chars. don't want re-invent wheel.
i wondering if there such thing can 'clean' strings using regex. instead of writing myself.
addition: if think cleaning string done differently/better i'm ears of course
another addition: - not spaces, kind of character.
edited based on update:
dirtystring.replaceall("[^a-za-z0-9]","")
Comments
Post a Comment