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

📄 stringutil.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package cn.myapps.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;


/**
 * The string utility.
 */
public class StringUtil {

    /**
     * Retrieve how many times is the substring in the larger string. Null returns 0. 
     * @param s the string to check
     * @param sb the substring to count
     * @return the number of occurances, 0 if the string is null
     */
    public static int countMatches(String s, String sb) {
        if (s == null || sb == null) {
            return 0;
        }
        int count = 0;
        int idx = 0;
        while ((idx = s.indexOf(sb, idx)) != -1) {
            count++;
            idx += sb.length();
        }
        return count;
    }

    /**
     * Gets the leftmost n characters of a string. If n characters are not
     * available, or the string is null, the string will be returned without an
     * exception.  
     * @param s The string to get the leftmost characters from
     * @param len The length of the required string
     * @return The leftmost characters
     */
    public static String left(String s, int len) {
        if (len < 0)
            throw new IllegalArgumentException("Requested String length " + len
                    + " is less than zero");
        return ((s == null) || (s.length() <= len)) ? s : s.substring(0, len);
    }

    /**
     * Gets the rightmost n characters of a string. If n characters are not
     * available, or the string is null, the string will be returned without an
     * exception. 
     * @param s The string to get the rightmost characters from
     * @param len The length of the required string
     * @return The leftmost characters
     */
    public static String right(String s, int len) {
        if (len < 0)
            throw new IllegalArgumentException("Requested String length " + len
                    + " is less than zero");
        return ((s == null) || (s.length() <= len)) ? s : s.substring(s
                .length()
                - len);
    }

    /**
     * Repeat a string n times to form a new string. 
     * @param s String to repeat
     * @param t The times of the string to repeat
     * @return The new string after repeat
     */
    public static String repeat(String s, int t) {
        StringBuffer buffer = new StringBuffer(t * s.length());
        
        for (int i = 0; i < t; i++)
            buffer.append(s);
        
        return buffer.toString();
    }

    /**
     * Right pad a String with spaces. Pad to a size of n. 
     * @param s String to repeat
     * @param z int number of times to repeat
     * @return right padded String
     */
    public static String rightPad(String s, int z) {
        return rightPad(s, z, " ");
    }

    /**
     * Right pad a String with a specified string. Pad to a size of n.
     * @param s The string to pad out
     * @param z The size to pad to
     * @param d The string to pad with
     * @return The right padded String
     */
    public static String rightPad(String s, int z, String d) {
        z = (z - s.length()) / d.length();
        if (z > 0)
            s += repeat(d, z);
        return s;
    }

    /**
     * Left pad a String with spaces. Pad to a size of n. 
     * @param s The String to pad out
     * @param z The size to pad to
     * @return The left padded String
     */
    public static String leftPad(String s, int z) {
        return leftPad(s, z, " ");
    }

    /**
     * Left pad a String with a specified string. Pad to a size of n.
     * @param s The String to pad out
     * @param z The size to pad to
     * @param d String to pad with
     * @return left padded String
     */
    public static String leftPad(String s, int z, String d) {
        z = (z - s.length()) / d.length();
        if (z > 0)
            s = repeat(d, z) + s;
        return s;
    }

    /**
     * Replace a string with another string inside a larger string, once. 
     * @see #replace(String text, String repl, String with, int max)
     * @param text text to search and replace in
     * @param repl The string to search for
     * @param with The String to replace with
     * @return The text with any replacements processed
     */
    public static String replaceOnce(String text, String repl, String with) {
        return replace(text, repl, with, 1);
    }

    /**
     * Replace all occurances of a string within another string. 
     * @see #replace(String text, String repl, String with, int max)
     * @param text The text to search and replace in
     * @param repl The String to search for
     * @param with THE String to replace with
     * @return The text with any replacements processed
     */
    public static String replace(String text, String repl, String with) {
        return replace(text, repl, with, -1);
    }

    /**
     * Replace a string with another string inside a larger string, for the
     * first <code>max</code> values of the search string. A <code>null</code>
     * reference is passed to this method is a no-op.
     * @param text The text to search and replace in
     * @param repl The String to search for
     * @param with The String to replace with
     * @param max The maximum number of values to replace, or <code>-1</code> if
     *            no maximum
     * @return The text with any replacements processed
     */
    public static String replace(String text, String repl, String with, int max) {
        if (text == null)
            return null;

        StringBuffer buf = new StringBuffer(text.length());
        int start = 0, end = 0;

        while ((end = text.indexOf(repl, start)) != -1) {
            buf.append(text.substring(start, end)).append(with);
            start = end + repl.length();

            if (--max == 0) {
                break;
            }
        }

        buf.append(text.substring(start));
        return buf.toString();
    }

    /**
     * Splits the provided text into a list, using whitespace as the separator.
     * The separator is not included in the returned String array. 
     * @param str The string to parse
     * @return An array of parsed Strings
     * @see #split(String, String, int)
     */
    public static String[] split(String str) {
        return split(str, null, -1);
    }

    /**
     * Splits the provided text into a list, using whitespace as the separator.
     * The separator is not included in the returned String array. 
     * @param text The string to parse
     * @param separator The Characters used as the delimiters.
     * @return An array of parsed Strings
     * @see #split(String, String, int)
     */
    public static String[] split(String text, String separator) {
        return split(text, separator, -1);
    }

    /**
     * Splits the provided text into a list, using whitespace as the separator.
     * The separator is not included in the returned String array.  
     * @param text  The string to parse
     * @param separator  The Characters used as the delimiters.
     * @return An array of parsed Strings
     * @see #split(String, String, int)
     */
    public static String[] split(String text, char separator) {
        return split(text, String.valueOf(separator));
    }

    /**
     * Splits the provided text into a list, based on a given separator. The
     * separator is not included in the returned String array. The maximum
     * number of splits to perfom can be controlled. A null separator will cause
     * parsing to be on whitespace. 
     * <p>This is useful for quickly splitting a string directly into an array of
     * tokens, instead of an enumeration of tokens (as<code>StringTokenizer</code> does).
     * @param str The string to parse.
     * @param separator Characters used as the delimiters. If <code>null</code>,
     *            splits on whitespace.
     * @param max The maximum number of elements to include in the list. A zero
     *            or negative value implies no limit.
     * @return an array of parsed Strings
     */
    public static String[] split(String str, String separator, int max) {
        StringTokenizer tok = null;
        if (separator == null) {
            tok = new StringTokenizer(str);
        } else {
            tok = new StringTokenizer(str, separator);
        }

        int listSize = tok.countTokens();
        if (max > 0 && listSize > max) {
            listSize = max;
        }

        String[] list = new String[listSize];
        int i = 0;
        int lastTokenBegin = 0;
        int lastTokenEnd = 0;

        while (tok.hasMoreTokens()) {
            if (max > 0 && i == listSize - 1) {
                String endToken = tok.nextToken();
                lastTokenBegin = str.indexOf(endToken, lastTokenEnd);
                list[i] = str.substring(lastTokenBegin);
                break;
            } else {
                list[i] = tok.nextToken();
                lastTokenBegin = str.indexOf(list[i], lastTokenEnd);
                lastTokenEnd = lastTokenBegin + list[i].length();
            }
            i++;
        }
        return list;
    }

    /**
     * Checks whether the String a valid Java Boolean.
     * Null and blank string will return false.
     * @param s the string to check
     * @return true if the string is a correctly Boolean
     */
    static public boolean isBoolean(String s) {
        if (s == null || s.trim().length() == 0)
            return false;
        return Boolean.valueOf(s.trim()).booleanValue();
    }

    /**
     * Check the string is the date format. 
     * @param s The string to check
     * @return true if the string is a correctly date 
     */
    public static boolean isDate(String s) {
        if (s == null || s.trim().length() == 0)
            return false;

        try {
            DateUtil.parseDate(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * Check the string is the date time format. 
     * @param s The string to check
     * @return true if the string is a correctly date 
     */
    public static boolean isDateTime(String s) {
        if (s == null || s.trim().length() == 0)
            return false;

        try {
            DateUtil.parseDateTime(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * Check the string is the time format. 
     * @param s The string to check
     * @return true if the string is a correctly date 
     */
    public static boolean isTime(String s) {
        if (s == null || s.trim().length() == 0) {

⌨️ 快捷键说明

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