StringUtil { public static String removeSpecialChars( String str, boolean removeSpaces ) { if ( StringUtils.isBlank( str ) ) { return ""; } else { if ( removeSpaces ) { return str.replaceAll( "[^a-zA-Z0-9._-]", "" ).trim(); } else { return str.replaceAll( "[^a-zA-Z0-9\\s._-]", "" ).trim(); } } } private  StringUtil(); static String escapeHtml( String str ); static boolean isBase64( String str ); static boolean isValidSshPublicKey( String sshKey ); static String unEscapeHtml( String str ); static boolean containsHtml( String str ); static String removeHtml( String str ); static String removeHtmlAndSpecialChars( String str, boolean removeSpaces ); static String removeSpecialChars( String str, boolean removeSpaces ); static boolean isValidEmail( String email );  }
@Test public void testRemoveSpecialChars() { String resultSpace = StringUtil.removeSpecialChars( htmlString, false ); String resultNoSpace = StringUtil.removeSpecialChars( htmlString, true ); assertFalse( resultSpace.contains( "<" ) ); assertTrue( resultSpace.contains( "script" ) ); assertTrue( resultSpace.contains( " " ) ); assertFalse( resultNoSpace.contains( " " ) ); }