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

📄 strutility.java

📁 一个完整的网络订餐系统
💻 JAVA
字号:
package com.util;

import java.util.*;


public class StrUtility
{
  /* private static MessageDigest digest = null;*/
  private static final char zeroArray[] = "0000000000000000".toCharArray();
  public StrUtility() {
  }

  public static String replaceString(String strSource, String strMatch,
                                     String strReplace) {
    String strRet = "";
    if (strSource != null) {
      int nLen = strSource.length();
      int nHead = 0;
      int nTail = nLen - 1;
      int nMatLen = strMatch.length();
      for (; nHead < nLen; nHead = nTail + nMatLen) {
        nTail = strSource.indexOf(strMatch, nHead);
        if (nTail == -1) {
          strRet = strRet + strSource.substring(nHead, nLen);
          break;
        }
        strRet = strRet + strSource.substring(nHead, nTail);
        strRet = strRet + strReplace;
      }
    }
    return strRet;
  }

  public static String replacenull(String sSoucre) {
    String sTemp;
    if (null == sSoucre || ("null").equals(sSoucre))
      sTemp = "";
    else
      sTemp = sSoucre;
    return sTemp;
  }

  public static final String dateToMillis(Date date) {
    return zeroPadString(Long.toString(date.getTime()), 15);
  }

  public static final byte[] decodeHex(String hex) {
    char chars[] = hex.toCharArray();
    byte bytes[] = new byte[chars.length / 2];
    int byteCount = 0;
    for (int i = 0; i < chars.length; i += 2) {
      byte newByte = 0;
      newByte |= hexCharToByte(chars[i]);
      newByte <<= 4;
      newByte |= hexCharToByte(chars[i + 1]);
      bytes[byteCount] = newByte;
      byteCount++;
    }

    return bytes;
  }

  public static final String encodeHex(byte bytes[]) {
    StringBuffer buf = new StringBuffer(bytes.length * 2);
    for (int i = 0; i < bytes.length; i++) {
      if ( (bytes[i] & 0xff) < 16)
        buf.append("0");
      buf.append(Long.toString(bytes[i] & 0xff, 16));
    }

    return buf.toString();
  }

  public static String getStr(String str) throws Exception {
    try {
      String temp_p = str;
      temp_p = new String(str.getBytes("GBK"), "ISO8859_1");
      return temp_p;
    }
    catch (Exception e) {
      System.out.println("Char chansform error:" + e);
    }
    return null;
  }

  public static String getStr2(String str) throws Exception {
    try {
      String temp_p = str;
      temp_p = new String(str.getBytes("ISO8859_1"));
      return temp_p;
    }
    catch (Exception e) {
      System.out.println("Char chansform error:" + e);
    }
    return null;
  }

  public static final synchronized String hash(String data) {
    if (digest == null)
      try {
        digest = MessageDigest.getInstance("MD5");
      }
      catch (NoSuchAlgorithmException nsae) {
        System.err.println("Failed to load the MD5 MessageDigest. Jive will be unable to function normally.");
        nsae.printStackTrace();
      }
    digest.update(data.getBytes());
    return encodeHex(digest.digest());
  }

  private static final byte hexCharToByte(char ch) {
    switch (ch) {
      case 48: // '0'
        return 0;

      case 49: // '1'
        return 1;

      case 50: // '2'
        return 2;

      case 51: // '3'
        return 3;

      case 52: // '4'
        return 4;

      case 53: // '5'
        return 5;

      case 54: // '6'
        return 6;

      case 55: // '7'
        return 7;

      case 56: // '8'
        return 8;

      case 57: // '9'
        return 9;

      case 97: // 'a'
        return 10;

      case 98: // 'b'
        return 11;

      case 99: // 'c'
        return 12;

      case 100: // 'd'
        return 13;

      case 101: // 'e'
        return 14;

      case 102: // 'f'
        return 15;

      case 58: // ':'
      case 59: // ';'
      case 60: // '<'
      case 61: // '='
      case 62: // '>'
      case 63: // '?'
      case 64: // '@'
      case 65: // 'A'
      case 66: // 'B'
      case 67: // 'C'
      case 68: // 'D'
      case 69: // 'E'
      case 70: // 'F'
      case 71: // 'G'
      case 72: // 'H'
      case 73: // 'I'
      case 74: // 'J'
      case 75: // 'K'
      case 76: // 'L'
      case 77: // 'M'
      case 78: // 'N'
      case 79: // 'O'
      case 80: // 'P'
      case 81: // 'Q'
      case 82: // 'R'
      case 83: // 'S'
      case 84: // 'T'
      case 85: // 'U'
      case 86: // 'V'
      case 87: // 'W'
      case 88: // 'X'
      case 89: // 'Y'
      case 90: // 'Z'
      case 91: // '['
      case 92: // '\\'
      case 93: // ']'
      case 94: // '^'
      case 95: // '_'
      case 96: // '`'
      default:
        return 0;
    }
  }

  public static final String replace(String line, String oldString,
                                     String newString) {
    if (line == null)
      return null;
    int i = 0;
    if ( (i = line.indexOf(oldString, i)) >= 0) {
      char line2[] = line.toCharArray();
      char newString2[] = newString.toCharArray();
      int oLength = oldString.length();
      StringBuffer buf = new StringBuffer(line2.length);
      buf.append(line2, 0, i).append(newString2);
      i += oLength;
      int j;
      for (j = i; (i = line.indexOf(oldString, i)) > 0; j = i) {
        buf.append(line2, j, i - j).append(newString2);
        i += oLength;
      }

      buf.append(line2, j, line2.length - j);
      return buf.toString();
    }
    else {
      return line;
    }
  }

  public static final String zeroPadString(String string, int length) {
    if (string == null || string.length() > length) {
      return string;
    }
    else {
      StringBuffer buf = new StringBuffer(length);
      buf.append(zeroArray, 0, length - string.length()).append(string);
      return buf.toString();
    }
  }

  public static final String  getCurrentDate()
 {
     Calendar now = new GregorianCalendar();
     int year = now.get(1);
     int month = now.get(2) + 1;
     int date = now.get(5);
     return String.valueOf(String.valueOf((new StringBuffer("")).append(year).append("-").append(month).append("-").append(date)));
 }

 public static final String cutString(String str, int num){
 int length = str.length();
 if(length>num){
   str = str.substring(0,num) + "...";
 }
 return str;
}

}

⌨️ 快捷键说明

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