java Source

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
            1/*
            2 * StringUtils.java
            3 * Copyright (C) 2007-3-19  <JustinLei@gmail.com>
            4 *
            5 *        This program is free software; you can redistribute it and/or modify
            6 *        it under the terms of the GNU General Public License as published by
            7 *      the Free Software Foundation; either version 2 of the License, or
            8 *     (at your option) any later version.
            9 *
           10 *       This program is distributed in the hope that it will be useful,
           11 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
           12 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           13 *        GNU General Public License for more details.
           14 *
           15 */

           16package org.lambdasoft.utils;
           17
           18import java.text.SimpleDateFormat;
           19import java.util.ArrayList;
           20import java.util.Date;
           21import java.util.List;
           22import java.util.StringTokenizer;
           23
           24import org.apache.commons.logging.Log;
           25import org.apache.commons.logging.LogFactory;
           26
           27/**
           28 * 字符串處理工具
           29 * 
           30 * @author TangLei <justinlei@gmail.com>
           31 * @date 2008-11-22
           32 */

           33public class StringUtils {
           34    private final static Log log = LogFactory.getLog(StringUtils.class);
           35    private StringUtils(){}
           36    
           37    /**
           38     * 判斷字符串是否為空
           39     * 
           40     * @param str    需要判斷的字符串
           41     * @return booleanValue 返回是否為空
           42     */

           43    public final static boolean isEmpty(String str) {
           44        if(str == null)
           45            return true;
           46        str = str.trim();
           47        if(str.length() == 0)
           48            return true;
           49        return false;
           50    }

           51    
           52    /**
           53     * 判斷字符串是否為空
           54     * 
           55     * @param str    需要判斷的字符串
           56     * @return    booleanValue 返回是否為空
           57     */

           58    public final static boolean isNotEmpty(String str) {
           59        return ! isEmpty(str);
           60    }

           61    
           62    public final static long getLength(String str) {
           63        if(isEmpty(str))
           64            return 0;
           65        return str.trim().length();
           66    }

           67    
           68    /**
           69     * 獲取分割的字符串
           70     * 
           71     * @param str    需要分割的字符串
           72     * @param segment    分割字符串
           73     * @return    segments    分割好的字符串數組
           74     */

           75    public final static String[] getStringSegment(String str,String segment) {
           76        if(str == null || str.trim().length() == 0)
           77            return null;
           78        if(segment == null || segment.length() == 0)
           79            return null;
           80        StringTokenizer stringTokenizer = new StringTokenizer(str,segment);
           81        List<String> segs = new ArrayList<String>();
           82        while(stringTokenizer.hasMoreTokens()) {
           83            String _token = stringTokenizer.nextToken();
           84            segs.add(_token);
           85            if(log.isDebugEnabled()) {
           86                log.debug("StringTokenizer.nextToken : " + _token);
           87            }

           88        }

           89        String[] returns = new String[segs.size()];
           90        for (int i = 0; i < segs.size(); i++{
           91            returns[i] = segs.get(i);
           92        }

           93        return returns;
           94    }

           95    
           96    /**
           97     * 時間格式化默認為(yyyy-MM-dd HH:mm:ss)
           98     * 
           99     * @param date
          100     * @param expression
          101     * @return
          102     */

          103    public static final String formatDate(Date date,String expression) {
          104        if(isEmpty(expression))
          105            expression = "yyyy-MM-dd HH:mm:ss";
          106        SimpleDateFormat sdf = new SimpleDateFormat(expression);
          107        return sdf.format(date);
          108    }

          109}

          110
          posted on 2009-12-18 14:28 JustinLei 閱讀(1368) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 瓮安县| 乳源| 嵩明县| 潼南县| 宝清县| 镇巴县| 兴山县| 陈巴尔虎旗| 治县。| 杭锦后旗| 兴城市| 襄城县| 同仁县| 余江县| 西乡县| 永登县| 徐州市| 平度市| 鹤峰县| 鹤壁市| 仁寿县| 榆中县| 黄陵县| 通海县| 威宁| 峨眉山市| 剑阁县| 临湘市| 重庆市| 元谋县| 阳西县| 将乐县| 虹口区| 武川县| 嘉义县| 临澧县| 喜德县| 丰原市| 中牟县| 马龙县| 昆明市|