posts - 33,  comments - 17,  trackbacks - 0
            1/**
            2 * Title:        Java Bean 工具
            3 * Description:
            4 * Copyright:    Copyright (c) 2001
            5 * Company:      JAVA世紀網 http://www.java2000.net
            6 * @author 趙學慶
            7 * @version 1.0
            8 */

            9import java.util.*;
           10import java.util.regex.Pattern;
           11
           12public class StrTools {
           13  /**
           14   * 分割字符串
           15   * 
           16   * @param str String 原始字符串
           17   * @param splitsign String 分隔符
           18   * @return String[] 分割后的字符串數組
           19   */

           20  @SuppressWarnings("unchecked")
           21  public static String[] split(String str, String splitsign) {
           22    int index;
           23    if (str == null || splitsign == null)
           24      return null;
           25    ArrayList al = new ArrayList();
           26    while ((index = str.indexOf(splitsign)) != -1{
           27      al.add(str.substring(0, index));
           28      str = str.substring(index + splitsign.length());
           29    }

           30    al.add(str);
           31    return (String[]) al.toArray(new String[0]);
           32  }

           33
           34  /**
           35   * 替換字符串
           36   * 
           37   * @param from String 原始字符串
           38   * @param to String 目標字符串
           39   * @param source String 母字符串
           40   * @return String 替換后的字符串
           41   */

           42  public static String replace(String from, String to, String source) {
           43    if (source == null || from == null || to == null)
           44      return null;
           45    StringBuffer bf = new StringBuffer("");
           46    int index = -1;
           47    while ((index = source.indexOf(from)) != -1{
           48      bf.append(source.substring(0, index) + to);
           49      source = source.substring(index + from.length());
           50      index = source.indexOf(from);
           51    }

           52    bf.append(source);
           53    return bf.toString();
           54  }

           55
           56  /**
           57   * 替換字符串,能能夠在HTML頁面上直接顯示(替換雙引號和小于號)
           58   * 
           59   * @param str String 原始字符串
           60   * @return String 替換后的字符串
           61   */

           62  public static String htmlencode(String str) {
           63    if (str == null{
           64      return null;
           65    }

           66
           67    return replace("\"""&quot;", replace("<""&lt;", str));
           68  }

           69
           70  /**
           71   * 替換字符串,將被編碼的轉換成原始碼(替換成雙引號和小于號)
           72   * 
           73   * @param str String
           74   * @return String
           75   */

           76  public static String htmldecode(String str) {
           77    if (str == null{
           78      return null;
           79    }

           80
           81    return replace("&quot;""\"", replace("&lt;""<", str));
           82  }

           83
           84  private static final String _BR = "<br/>";
           85
           86  /**
           87   * 在頁面上直接顯示文本內容,替換小于號,空格,回車,TAB
           88   * 
           89   * @param str String 原始字符串
           90   * @return String 替換后的字符串
           91   */

           92  public static String htmlshow(String str) {
           93    if (str == null{
           94      return null;
           95    }

           96
           97    str = replace("<""&lt;", str);
           98    str = replace(" ""&nbsp;", str);
           99    str = replace("\r\n", _BR, str);
          100    str = replace("\n", _BR, str);
          101    str = replace("\t""&nbsp;&nbsp;&nbsp;&nbsp;", str);
          102    return str;
          103  }

          104
          105  /**
          106   * 返回指定字節長度的字符串
          107   * 
          108   * @param str String 字符串
          109   * @param length int 指定長度
          110   * @return String 返回的字符串
          111   */

          112  public static String toLength(String str, int length) {
          113    if (str == null{
          114      return null;
          115    }

          116    if (length <= 0{
          117      return "";
          118    }

          119    try {
          120      if (str.getBytes("GBK").length <= length) {
          121        return str;
          122      }

          123    }
           catch (Exception ex) {
          124    }

          125    StringBuffer buff = new StringBuffer();
          126
          127    int index = 0;
          128    char c;
          129    length -= 3;
          130    while (length > 0{
          131      c = str.charAt(index);
          132      if (c < 128{
          133        length--;
          134      }
           else {
          135        length--;
          136        length--;
          137      }

          138      buff.append(c);
          139      index++;
          140    }

          141    buff.append("");
          142    return buff.toString();
          143  }

          144
          145  /**
          146   * 判斷是否為整數
          147   * 
          148   * @param str 傳入的字符串
          149   * @return 是整數返回true,否則返回false
          150   */

          151  public static boolean isInteger(String str) {
          152    Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
          153    return pattern.matcher(str).matches();
          154  }

          155
          156  /**
          157   * 判斷是否為浮點數,包括double和float
          158   * 
          159   * @param str 傳入的字符串
          160   * @return 是浮點數返回true,否則返回false
          161   */

          162  public static boolean isDouble(String str) {
          163    Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
          164    return pattern.matcher(str).matches();
          165  }

          166
          167  /**
          168   * 判斷輸入的字符串是否符合Email樣式.
          169   * 
          170   * @param str 傳入的字符串
          171   * @return 是Email樣式返回true,否則返回false
          172   */

          173  public static boolean isEmail(String str) {
          174    Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
          175    return pattern.matcher(str).matches();
          176  }

          177
          178  /**
          179   * 判斷輸入的字符串是否為純漢字
          180   * 
          181   * @param str 傳入的字符竄
          182   * @return 如果是純漢字返回true,否則返回false
          183   */

          184  public static boolean isChinese(String str) {
          185    Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
          186    return pattern.matcher(str).matches();
          187  }

          188
          189  /**
          190   * 是否為空白,包括null和""
          191   * 
          192   * @param str
          193   * @return
          194   */

          195  public static boolean isBlank(String str) {
          196    return str == null || str.trim().length() == 0;
          197  }

          198
          199  /**
          200   * 判斷是否為質數
          201   * 
          202   * @param x
          203   * @return
          204   */

          205  public static boolean isPrime(int x) {
          206    if (x <= 7{
          207      if (x == 2 || x == 3 || x == 5 || x == 7)
          208        return true;
          209    }

          210    int c = 7;
          211    if (x % 2 == 0)
          212      return false;
          213    if (x % 3 == 0)
          214      return false;
          215    if (x % 5 == 0)
          216      return false;
          217    int end = (int) Math.sqrt(x);
          218    while (c <= end) {
          219      if (x % c == 0{
          220        return false;
          221      }

          222      c += 4;
          223      if (x % c == 0{
          224        return false;
          225      }

          226      c += 2;
          227      if (x % c == 0{
          228        return false;
          229      }

          230      c += 4;
          231      if (x % c == 0{
          232        return false;
          233      }

          234      c += 2;
          235      if (x % c == 0{
          236        return false;
          237      }

          238      c += 4;
          239      if (x % c == 0{
          240        return false;
          241      }

          242      c += 6;
          243      if (x % c == 0{
          244        return false;
          245      }

          246      c += 2;
          247      if (x % c == 0{
          248        return false;
          249      }

          250      c += 6;
          251    }

          252    return true;
          253  }

          254
          255  public static void main(String[] args) {
          256    String[] numbers = "12345""-12345""123.45""-123.45"".12345""-.12345""a12345""12345a""123.a45" };
          257    for (String str : numbers) {
          258      System.out.println(str + "=" + isInteger(str) + " " + isDouble(str));
          259    }

          260
          261    String[] emails = "1@2.com""1.2@3.com""1@3.4.5.com" };
          262    for (String str : emails) {
          263      System.out.println(str + "=" + isEmail(str));
          264    }

          265    String[] chineses = "中國""1中國""中國1""1中國2""中1國" };
          266    for (String str : chineses) {
          267      System.out.println(str + "=" + isChinese(str));
          268    }

          269  }

          270}
          posted on 2008-07-23 18:01 scea2009 閱讀(238) 評論(0)  編輯  收藏 所屬分類: 網摘

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          PL/SQL存儲過程與函數

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 桂林市| 申扎县| 宿松县| 宁国市| 中山市| 建德市| 体育| 夏津县| 临沧市| 将乐县| 麻城市| 甘洛县| 开江县| 那曲县| 崇州市| 永胜县| 青冈县| 湖南省| 汝城县| 太湖县| 宿松县| 石家庄市| 吉木乃县| 双柏县| 松溪县| 出国| 叙永县| 大宁县| 米林县| 丰顺县| 密云县| 南江县| 安达市| 陆川县| 疏勒县| 二连浩特市| 金秀| 阳信县| 夏邑县| 营山县| 纳雍县|