<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 33,  comments - 17,  trackbacks - 0
      1/**
      2 * Title:        Java Bean 工具
      3 * Description:
      4 * Copyright:    Copyright (c) 2001
      5 * Company:      JAVA世紀(jì)網(wǎng) http://www.java2000.net
      6 * @author 趙學(xué)慶
      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[] 分割后的字符串?dāng)?shù)組
     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 目標(biāo)字符串
     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頁(yè)面上直接顯示(替換雙引號(hào)和小于號(hào))
     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   * 替換字符串,將被編碼的轉(zhuǎn)換成原始碼(替換成雙引號(hào)和小于號(hào))
     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   * 在頁(yè)面上直接顯示文本內(nèi)容,替換小于號(hào),空格,回車,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   * 返回指定字節(jié)長(zhǎng)度的字符串
    107   * 
    108   * @param str String 字符串
    109   * @param length int 指定長(zhǎng)度
    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   * 判斷是否為整數(shù)
    147   * 
    148   * @param str 傳入的字符串
    149   * @return 是整數(shù)返回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   * 判斷是否為浮點(diǎn)數(shù),包括double和float
    158   * 
    159   * @param str 傳入的字符串
    160   * @return 是浮點(diǎn)數(shù)返回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   * 判斷是否為質(zhì)數(shù)
    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 = "中國(guó)""1中國(guó)""中國(guó)1""1中國(guó)2""中1國(guó)" };
    266    for (String str : chineses) {
    267      System.out.println(str + "=" + isChinese(str));
    268    }

    269  }

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

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

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    PL/SQL存儲(chǔ)過(guò)程與函數(shù)

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲伊人久久大香线蕉苏妲己| 欧洲亚洲国产精华液| 一区二区三区免费电影| 国产91色综合久久免费分享| 亚洲中文字幕无码一久久区| 亚洲AV无码一区二区三区久久精品| 精品无码人妻一区二区免费蜜桃| 亚洲精品无码你懂的网站| 国产精品亚洲一区二区麻豆| 久久爰www免费人成| 国产成人亚洲综合| 国内成人精品亚洲日本语音| 18禁免费无码无遮挡不卡网站| 亚洲成AV人在线播放无码| 日本高清不卡中文字幕免费| 日韩在线看片免费人成视频播放| 亚洲精品资源在线| 久久青青草原国产精品免费| 免费一级特黄特色大片在线观看| 亚洲va久久久久| 国产免费丝袜调教视频| 久久伊人久久亚洲综合| www在线观看播放免费视频日本| 日本高清色本免费现在观看| 亚洲AV无码乱码在线观看代蜜桃| 污污网站免费观看| 国产日韩亚洲大尺度高清| yellow视频免费在线观看| 免费一级国产生活片| 亚洲日韩AV一区二区三区四区 | 一级毛片a免费播放王色| 日韩在线天堂免费观看| 亚洲伊人久久大香线蕉结合| 5g影院5g天天爽永久免费影院| 亚洲va中文字幕无码久久不卡| 亚洲免费无码在线| 亚洲精品视频久久久| 黄网站色视频免费观看45分钟| 日韩精品免费电影| 亚洲成a人片在线观看天堂无码 | 性做久久久久久久免费看|