⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringhelper.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.utils;import com.queplix.core.error.GenericSystemException;import org.apache.regexp.RE;import org.apache.regexp.RESyntaxException;import java.io.PrintWriter;import java.io.StringWriter;import java.text.MessageFormat;import java.text.ParseException;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.List;import java.util.StringTokenizer;/** * Helper class for string manipulations. * * @author [ONZ] Oleg N. Zhovtanyuk * @author [ALB] Baranov Andrey * @version $Revision: 1.5 $ $Date: 2006/06/13 19:30:08 $ */public final class StringHelper {    // =============================================================== Constants    /** Default empty string value. */    public static final String EMPTY_VALUE = "";    /** Default empty integer value. */    public static final byte EMPTY_NUMBER = -1;    /** Default value for <b>null</b> strings. */    public static final String NULL_VALUE = "null";    /** End line characters sequence. */    public static final String CRLF = "\r\n";    /** Default delimeter. */    public static final String DEFAULT_DELIMETER = ",;\n\r";    public static final String HTML_INDICATOR = "<!--html-->";    // ========================================================== Initialization    // Private constructor - blocks instantiation.    private StringHelper() {}    // ================================================================= Methods    /**     * Trims the string, returns <b>null</b> if empty.     *     * @param s the string to trim     * @return the trimmed string or <b>null</b>, if empty     */    public static String trim( String s ) {        if( s != null ) {            s = s.trim();            if( s.length() == 0 ) {                s = null;            }        }        return s;    }    /**     * Trims the <code>StringBuffer</code>, returns <b>null</b> if empty.     *     * @param sb the <code>StringBuffer</code> to trim     * @return the trimmed <code>StringBuffer</code> or <b>null</b>, if empty     */    public static String trim( StringBuffer sb ) {        if( sb == null || sb.length() == 0 ) {            return null;        } else {            return trim( sb.toString() );        }    }    /**     * Checks the string for emptiness.     * <b>Null</b> and the string of space characters are treated as empty.     *     * @param s the string to check     * @return <b>true</b> if empty     */    public static boolean isEmpty( String s ) {        return( s == null ) ? true : ( s.trim().length() == 0 );    }    /**     * Checks the character array for emptiness.     * @param cc the array of characters to check     * @return <b>true</b> if empty     */    public static boolean isEmpty( char[] cc ) {        return( cc == null ) ? true : isEmpty( String.valueOf( cc ) );    }    /**     * Checks the integer value for emptiness.     *     * @param l integer to check     * @return <b>true</b> if <code>i == EMPTY_NUMBER</code>     */    public static boolean isEmpty( long l ) {        return l == EMPTY_NUMBER;    }    /**     * Compares two strings.     * Makes no difference between the <b>null</b> values and empty strings.     *     * @param s1 the first string     * @param s2 the second string     * @return <b>true</b> if they're equal     */    public static boolean cmp( String s1, String s2 ) {        if( isEmpty( s1 ) && isEmpty( s2 ) ) {            return true;        } else {            if( s1 == null || s2 == null ) {                return false;            } else {                return s1.equals( s2 );            }        }    }    /**     * Converts the string into boolean.     *     * <p>     * Rules:     * <ul>     *  <li><code>true</code> | <code>1</code> - <b>true</b></li>     *  <li><code>false</code> | <code>0</code> | null string - <b>false</b></li>     *  <li>all the rest - exception</li>     * </ul>     * </p>     *     * @param s the string to convert     * @return boolean value     * @throws ParseException     */    public static boolean str2bool( String s )        throws ParseException {        boolean value = false;        if( !isEmpty( s ) ) {            if( ( s.length() == 1 && s.equals( "1" ) ) || ( s.length() == 4 && s.equalsIgnoreCase( "true" ) ) ) {                value = true;            } else if( ( s.length() == 1 && s.equals( "0" ) ) || ( s.length() == 5 && s.equalsIgnoreCase( "false" ) ) ) {                return false;            } else {                throw new ParseException( "Can't convert the string to boolean", 0 );            }        }        return value;    }    /**     * Converts the string into Integer removing all none-digits from the String     *     * @param s the string to convert     * @return Integer value     * @throws ParseException     */    public static Integer str2integer( String s )        throws ParseException {        Integer value = new Integer (0);                         if( !isEmpty( s ) ) {            try {                // Removing everything but digits from the String                RE re = new RE( "[^0-9]", RE.REPLACE_ALL );                s = re.subst( s, "");                  value = new Integer(s);            } catch (NumberFormatException e) {                throw new ParseException( "Can't convert string to integer: " + e.getMessage(), 0 );            } catch( RESyntaxException rex ) {                throw new GenericSystemException( "Can't convert string to integer, regexp exception: " + rex.getMessage(), rex );            }        }         return value;    }    /**     * Splits the string by the given delimiter.     *     * @param s the string to split     * @param delim delimiter     * @param returnDelims should it return delims or not.     * @return tokens array     */    public static String[] split(String s, String delim, boolean returnDelims) {        StringTokenizer st = new StringTokenizer( s, delim, returnDelims );        List<String> tokens = new ArrayList<String>( st.countTokens() );        while( st.hasMoreTokens() ) {            tokens.add( st.nextToken() );        }        String[] rc = new String[tokens.size()];        if( rc.length > 0 ) {            rc = tokens.toArray( rc );        }        return tokens.toArray(new String[tokens.size()]);    }    /**     * Splits the string by the given delimiter.     *     * @param s the string to split     * @param delim delimiter     * @param returnDelims should it return delims or not.     * @return tokens array     */    public static String[] split(String s, String delim) {        return split(s, delim, false);    }    /**     * Splits the string by the default delimiter.     *     * @param s the string to split     * @return collection of tokens (might be empty)     */    public static Collection split( String s ) {        // Check for empty string.        if( isEmpty( s ) ) {            return Collections.EMPTY_LIST;        }        // Is HTML?        if( isHTML( s ) ) {            s = html2text( s );        }        // Just split.        StringTokenizer st = new StringTokenizer( s, DEFAULT_DELIMETER );        List tokens = new ArrayList( st.countTokens() );        while( st.hasMoreTokens() ) {            String token = st.nextToken();            if( !isEmpty( token ) ) {                tokens.add( token.trim() );            }        }        // Ok.        return tokens.size() == 0 ? Collections.EMPTY_LIST : tokens;    }    /**     * Splits the string (presented as character array) by the default delimiter.     *     * @param cc character array to split     * @return collection of tokens (might be empty)     */    public static Collection split( char[] cc ) {        String s = ( cc != null ? String.valueOf( cc ) : EMPTY_VALUE );        return split( s );    }    /**     * Joins the array of strings into one string with the delimiter.     * Empty string are omitted.     *     * @param s the strings to join     * @param delim delimiter     * @return joined strings     */    public static String join( String[] s, String delim ) {        if( s == null || s.length == 0 ) {            return null;        }        StringBuffer sb = new StringBuffer();        int max = s.length - 1;        if( delim == null || delim.length() == 0 ) {            for( int i = 0; i <= max; i++ ) {                if( !isEmpty( s[i] ) ) {                    sb.append( s[i] );                }            }        } else {            for( int i = 0; i <= max; i++ ) {                if( !isEmpty( s[i] ) ) {                    sb.append( s[i] );                    if( i < max ) {                        sb.append( delim );                    }                }            }        }        return sb.toString();    }    /**     * Joins the array of strings into one string with the delimiter.     * Empty strings are included.     *     * @param s the strings to join     * @param delim delimiter     * @return joined strings     */    public static String fullJoin( String[] s, String delim ) {        // Data checks.        if( s == null || s.length == 0 ) {            return null;        } else if( delim == null || delim.length() == 0 ) {            throw new IllegalArgumentException( "Delimiter is empty" );        }        // Joining...        StringBuffer sb = new StringBuffer();        int max = s.length - 1;        for( int i = 0; i <= max; i++ ) {            if( s[i] == null ) {                sb.append( NULL_VALUE );            } else {                sb.append( '\'' ).append( s[i] ).append( '\'' );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -