📄 stringhelper.java
字号:
} else { return result; } } /** * Removes system HTML tags (<html>,<body>,<head>...) from HTML. * @param s the string to convert * @return cleared HTML */ public static String clearHtml( String s ) { return clearHtml( s, true ); } /** * Removes system HTML tags (<html>,<body>,<head>...) from HTML. * @param s the string to convert * @param clearText clear bad characters * @return cleared HTML */ public static String clearHtml( String s, boolean clearText ) { if( isEmpty( s ) ) { return s; } String result = s; try { // Removes <!DOCTYPE>, <HTML> and <BODY> tags. RE re = new RE( "<!DOCTYPE[^>]*>|<HTML[^>]*>|<BODY[^>]*>|<\\/HTML>|<\\/BODY>", RE.REPLACE_ALL | RE.MATCH_CASEINDEPENDENT | RE.MATCH_MULTILINE ); result = re.subst( result, "\n" ); // Removes <HEAD> <SCRIPT> ... - with content. // [MVT] Excludes <STYLE> from processing to provide proper email body display. String[] _ss = new String[] {"HEAD", "SCRIPT"}; for( int i = 0; i < _ss.length; i++ ) { String _s = _ss[i]; re = new RE( "<" + _s + "[^>]*>", RE.MATCH_CASEINDEPENDENT | RE.MATCH_MULTILINE ); if( re.match( result ) ) { int start = re.getParenStart( 0 ); re = new RE( "<\\/" + _s + ">", RE.MATCH_CASEINDEPENDENT ); if( re.match( result, start ) ) { int end = re.getParenEnd( 0 ); result = result.substring( 0, start ) + result.substring( end ); } } } } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } if( clearText ) { // Removes "bad" characters. return clear( result ); } else { return result; } } /** * Checks whether the given string is XML. * @param s the string to check * @return <b>true</b> if XML */ public static boolean isXML( String s ) { if( isEmpty( s ) ) { return false; } try { RE re = new RE( "<\\?xml((\\s+[^>]*)|(>))", RE.MATCH_CASEINDEPENDENT ); return re.match( s ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } } /** * Checks whether the given character array is XML. * @param chs the character array to check * @return <b>true</b> if XML */ public static boolean isXML( char[] chs ) { if( chs == null ) { return false; } return isXML( String.valueOf( chs ) ); } /** * Checks whether the given string is HTML. * @param s the string to check * @return <b>true</b> if HTML */ public static boolean isHTML( String s ) { if( isEmpty( s ) ) { return false; } try { RE re = new RE( "<html((\\s+[^>]*)|(>))", RE.MATCH_CASEINDEPENDENT ); if(re.match( s )) return true; if(s.indexOf(HTML_INDICATOR) != -1) return true; else return false; } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } } /** * Checks whether the given character array is HTML. * @param chs the character array to check * @return <b>true</b> if HTML */ public static boolean isHTML( char[] chs ) { if( chs == null ) { return false; } return isHTML( String.valueOf( chs ) ); } /** * Performs a case-independent search for the substring in the given string. * Returns <code>false</code>, if any parameter is empty. * * @param s the string to search in * @param substring the substring to search for * * @return <b>true</b> if found */ public static boolean find( String s, String substring ) { boolean result = false; if( isEmpty( s ) || isEmpty( substring ) ) { result = false; } else { result = ( s.toLowerCase().indexOf( substring.toLowerCase() ) >= 0 ); } return result; } /** * Converts the string from SQL to Java format. * * <p> * If the string is empty or contains just 1 character it is not changed. * Otherwise all double quotas are replaced with the single ones. * </p> * * @param s the string to convert * @return converted string * * @see #java2sql(String) */ public static String sql2java( String s ) { if( isEmpty( s ) || s.length() < 2 ) { return s; } s = s.substring( 1, s.length() - 1 ); String result = s; try { RE re = new RE( "''", RE.REPLACE_ALL ); result = re.subst( result, "'" ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } return result; } /** * Converts the string from Java to SQL format. * * @param s the string to convert * @return converted string * * @see #sql2java(String) */ public static String java2sql( String s ) { if( isEmpty( s ) ) { return "''"; } String result = s; try { RE re = new RE( "'", RE.REPLACE_ALL ); result = re.subst( result, "''" ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } return "'" + result + "'"; } /** * Formats the message using <code>MessageFormat</code> class * @param pattern the message pattern to format * @param args arguments * @return formatted string */ public static String format( String pattern, Object[] args ) { if( args != null ) { Object[] __args = new Object[args.length]; for( int i = 0; i < args.length; i++ ) { Object o = args[i]; if( o == null ) { o = NULL_VALUE; } __args[i] = o; } pattern = MessageFormat.format( pattern, __args ); } return pattern; } /** * Make the printable string (e. g. <code>[e1, e2, ...]</code>) * from the integer array. * * @param l the integer array * @return the printable string */ public static String toString( long[] l ) { String str = NULL_VALUE; if( l != null && l.length > 0 ) { StringBuffer sb = new StringBuffer(); sb.append( "[" ); sb.append( l[0] ); for( int i = 0; i < l.length; i++ ) { sb.append( ", " ); sb.append( l[i] ); } sb.append( "]" ); str = sb.toString(); } return str; } /** * Make the printable string (e. g. <code>[e1, e2, ...]</code>) * from the string array. * * @param s the string array * @return the printable string */ public static String toString( String[] s ) { String str = NULL_VALUE; if( s != null && s.length > 0 ) { str = "[" + fullJoin( s, ", " ) + "]"; } return str; } /** * Replaces all regular expression matches with the given replacement text. * * @param s string to process * @param regex regular expression * @param replacement replacement text * @return modified string */ public static String replaceFirst( String s, String regex, String replacement ) { if (s == null) return null; try { RE re = new RE( regex, RE.REPLACE_FIRSTONLY ); return re.subst( s, replacement ); } catch( RESyntaxException ex ) { throw new GenericSystemException( "RE exception: " + ex.getMessage(), ex ); } } /** * Replaces first regular expression match with the given replacement text. * * @param s string to process * @param regex regular expression * @param replacement replacement text * @return modified string */ public static String replaceAll( String s, String regex, String replacement ) { if (s == null) return null; try { RE re = new RE( regex, RE.REPLACE_ALL ); return re.subst( s, replacement ); } catch( RESyntaxException ex ) { throw new GenericSystemException( "RE exception: " + ex.getMessage(), ex ); } } /** * Converts the XML entities to ANSI symbols. * * @param s the string to convert * @return new string */ public static String replaceXMLEntities( String s ) { if( isEmpty( s ) ) { return s; } String result = s; try { // Non-blanking spaces. RE re = new RE( " ", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, " " ); // Entity for 'greater' sign. re = new RE( ">", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, ">" ); // Entity for 'lesser' sign. re = new RE( "<", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "<" ); // Entity for 'quotation' sign. re = new RE( """, RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "\"" ); // Entity for 'apostrof' sign. re = new RE( "'", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "'" ); // Entity for 'lesser' sign. re = new RE( "&", RE.MATCH_CASEINDEPENDENT + RE.REPLACE_ALL ); result = re.subst( result, "&" ); } catch( RESyntaxException rex ) { throw new GenericSystemException( "Regexp exception: " + rex.getMessage(), rex ); } return result; } public static String getExceptionStacktrace(Throwable cause) { StringWriter stringWriter = new StringWriter(); cause.printStackTrace(new PrintWriter(stringWriter, true)); return stringWriter.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -