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

📄 stringutils.java

📁 基于struts框架的web开发应用实例
💻 JAVA
字号:
package com.hp.mw.samples.struts.storefront.utils;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;


/**
 * This class is used to manipulate strings
 */
public class StringUtils
{
    //~ Static fields/initializers ---------------------------------------------

    private static final String PROPERTY_START_TOKEN = "${";
    private static final String PROPERTY_END_TOKEN = "}";

    //~ Methods ----------------------------------------------------------------

    public static String capitalize( String original )
    {
        if ( StringUtils.nullOrEmpty( original ) )
        {
            return original;
        }

        return original.substring( 0, 1 ).toUpperCase(  ) + original.substring( 1 );
    }

    /**
     * Performs a string replace
     */
    public static String replace( String original,
                                  String find,
                                  String substitute )
    {
        String result = original;
        int    location = original.indexOf( find );

        while ( location >= 0 )
        {
            result   = result.substring( 0, location ) + substitute + result.substring( location + find.length(  ) );
            location = result.indexOf( find, location + find.length(  ) );
        }

        return result;
    }

    /**
     * Returns <code>true</code> if the string is null or zero length
     */
    public static boolean nullOrEmpty( String value )
    {
        return ( value != null )
               ? ( value.length(  ) == 0 )
               : true;
    }

    /**
         * Returns <code>true</code> if the string "str" is contained in string "in".
     * null does not contain null and an empty string does not contain an emptry string.
     * no string contains an empty string
     */
    public static boolean containsString( String in,
                                          String str )
    {
        return ( ( ( str == null ) || ( in == null ) || ( in.length(  ) == 0 ) || ( str.length(  ) == 0 ) )
                 ? false
                 : ( in.indexOf( str ) >= 0 ) );
    }

    /**
     * Returns the strings separated by the separator string.  If the initial
     * string is empty then the separator is not used.
     */
    public static String concat( String str1,
                                 String str2,
                                 char   concatenator )
    {
        if ( nullOrEmpty( str1 ) )
        {
            return str2;
        }
        else if ( nullOrEmpty( str2 ) )
        {
            return str1;
        }
        else
        {
            return ( str1 + concatenator + str2 );
        }
    }

    /**
     * Strips spaces from a string, including internal spaces.
     *
     * @param str string to strip
     * @return string with no spaces
     */
    public static String trim( String str )
    {
        return trim( str, true, false );
    }

    /**
     * Strips spaces from a string, including internal spaces.
     *
     * @param str string to strip
     * @return string with no spaces
     */
    public static String trim( String  str,
                               boolean trimSpaces,
                               boolean trimNonAlpha )
    {
        if ( ( str == null ) || str.equals( "" ) )
        {
            return "";
        }

        int          length = str.length(  );
        StringBuffer sb = new StringBuffer( length );

        for ( int i = 0; i < length; i++ )
        {
            char c = str.charAt( i );

            if ( ( trimSpaces && Character.isSpaceChar( c ) ) || ( trimNonAlpha && !Character.isLetterOrDigit( c ) ) )
            {
                continue;
            }

            sb.append( c );
        }

        String key = sb.toString(  );

        return key.trim(  );
    }

    public static boolean getBoolean( String  value,
                                      boolean defaultValue )
    {
        try
        {
            return Boolean.valueOf( value ).booleanValue(  );
        }
        catch ( Exception e )
        {
            return defaultValue;
        }
    }

    public static int getInt( String value,
                              int    defaultValue )
    {
        try
        {
            return Integer.valueOf( value ).intValue(  );
        }
        catch ( Exception e )
        {
            return defaultValue;
        }
    }

    public static float getFloat( String value,
                                  float  defaultValue )
    {
        try
        {
            return Float.valueOf( value ).floatValue(  );
        }
        catch ( Exception e )
        {
            return defaultValue;
        }
    }

    public static double getDouble( String value,
                                    double defaultValue )
    {
        try
        {
            return Double.valueOf( value ).doubleValue(  );
        }
        catch ( Exception e )
        {
            return defaultValue;
        }
    }

    private static boolean replaceProperty( Properties   properties,
                                            String       value,
                                            StringBuffer buffer,
                                            int          startTokenIndex,
                                            String       startToken,
                                            String       propertyEndToken )
    {
        int endTokenIndex = value.indexOf( propertyEndToken );
        if ( endTokenIndex < 0 )
        {
            return false;
        }

        String propertyName = value.substring( startTokenIndex + startToken.length(  ), endTokenIndex );
        String propertyValue = properties.getProperty( propertyName );
        if ( propertyValue == null )
        {
            propertyValue = "";
        }

        buffer.replace( startTokenIndex, endTokenIndex + 1, propertyValue );
        return true;
    }

    public static String resolveProperties( Properties properties,
                                            String     value,
                                            String     startToken,
                                            String     endToken )
    {
        if ( nullOrEmpty( value ) || nullOrEmpty( startToken ) || nullOrEmpty( endToken ) )
        {
            return value;
        }

        StringBuffer buffer = new StringBuffer( value );
        while ( true )
        {
            int startTokenIndex = value.indexOf( startToken );
            if ( startTokenIndex < 0 )
            {
                break;
            }

            if ( !replaceProperty( properties, value, buffer, startTokenIndex, startToken, endToken ) )
            {
                break;
            }

            value = buffer.toString(  );
        }

        return buffer.toString(  );
    }

    public static String resolveProperties( String value )
    {
        return resolveProperties( System.getProperties(  ), value );
    }

    public static String resolveProperties( Properties properties,
                                            String     value )
    {
        return resolveProperties( properties, value, PROPERTY_START_TOKEN, PROPERTY_END_TOKEN );
    }

    public static void resolveProperties( Properties props )
    {
        Enumeration keys = props.keys(  );
        if ( keys == null )
        {
            return;
        }

        while ( keys.hasMoreElements(  ) )
        {
            String key = ( String ) keys.nextElement(  );
            String value = props.getProperty( key );
            props.setProperty( key, StringUtils.resolveProperties( props, value ) );
        }
    }

    public static String[] toArray(String source, String delimiter)
    {
        List results = new ArrayList();
        if (source != null) {
	        StringTokenizer tokenizer = new StringTokenizer(source, delimiter);
	        while (tokenizer.hasMoreTokens()) {
	            String token = tokenizer.nextToken();
	            results.add(token.trim());
	        }
        }

        return (String[])results.toArray(new String[0]);
    }


}

⌨️ 快捷键说明

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