对String的操作

/*
 * 创建日期 2004-11-3
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package com.pub.str;

/**
 * 处理字符串相关功能的类
 * <p>Copyright: 2004</p>
 * @author explorerwg
 * @version 1.1
 */
public class StringFunction {
 private String str;

 public StringFunction(String _Str) {
  str = _Str;
 }

 /**
  * 判别两个字符串是否匹配,其中'*'代表任意长的字符串(包括0长字符串),'?'代表单字节字符
  * @param _Str 传入的字串
  * @return 若不匹配,则返回false
  */
 public boolean like(String _Str) {
  return str.matches(".*" + _Str + ".*");
 }

 public synchronized static boolean like(String str1, String str2) {
  return new StringFunction(str1).like(str2);
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 将字符串扩充到长度为num,如果超过长度则去除超过的部分,否则则在前补0
  * @param num 指定扩充到的长度
  * @return 返回的字串
  */
 public String updateZero(int num) {
  return updateChar(num, '0');
 }

 public synchronized static String updateZero(String str, int num) {
  return new StringFunction(str).updateZero(num);
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 将字符串扩充到长度为num,如果超过长度则去除超过的部分,
  * 否则则在前面不上某个字符
  * @param num 指定扩充到的长度
  * @param c 补上的字符
  * @return 返回的字串
  */
 public String updateChar(int num, char c) {
  if (str.length() == num) {
   return str;
  } else if (str.length() < num) {
   for (int i = str.length(); i < num; i++) {
    str = c + str;
   }
   return str;
  } else {
   return str.substring(str.length() - num);
  }
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 如果字符串不为null,那么就在两面加上"",
  * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代
  * @param s 传入的字串
  * @return 返回的字串
  */
 public synchronized static String setNull(String s) {
  if (s != null) {
   if (!s.equals("")) {
    s = "'" + s + "'";
   } else {
    s = null;
   }
  }
  return s;
 }

 /**
  * 如果字符串为"",那么就把它设为null。
  * 该方法一般用于书写Sql语句中处理日期时,
  * 其功能已被SqlExpression中的类似方法所取代
  * @param s 传入的字串
  * @return 返回的字串
  */
 public synchronized static String setDateNull(String s) {
  if (s != null) {
   if (s.equals("")) {
    s = null;
   }
  }
  return s;
 }

 /**
  * 将数字转换为字符串,并在的两边加上引号,
  * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代
  * @param s 传入的数字
  * @return 返回的字串
  */
 public synchronized static String setNull(long s) {
  return "'" + String.valueOf(s) + "'";
 }

 /**
  * 将数字转换为字符串,并在的两边加上引号,
  * 该方法一般用于书写Sql语句,其功能可以被SqlExpression中的类似方法取代
  * @param s 传入的数字
  * @return 返回的字串
  */
 public synchronized static String setNull(double s) {
  return "'" + String.valueOf(s) + "'";
 }

 /**
  * 在字符串的末尾加上换行的符号
  * @param s 传入的字串
  * @return 返回的字串
  */
 public synchronized static String addReturn(String s) {
  return s + "/n";
 }

 /**
  * 如果字符串为空,则返回一个"&nbsp;"符号
  * @param s 传入的字串
  * @return 返回的字串
  */
 public synchronized static String getHtmlFormat(String s) {
  if (s != null) {
   if (s.trim().equals("")) {
    return "&nbsp;";
   } else {
    return s.trim();
   }
  } else {
   return "&nbsp;";
  }
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 如果传入的字符串为null,则返回一个""
  * @param s 传入的字串
  * @return 返回的字串
  */
 public synchronized static String getStringFormat(Object o) {
  if (o == null) {
   return "";
  } else {
   String s = o.toString();
   return s.trim();
  }
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 保证字符串可以转化为数值型,若不能则返回"0"
  * @param s 传入的字串
  * @return 返回的字串
  */
 public synchronized static String getNumberFormat(Object o) {
  if (o == null) {
   return "0";
  }
  String s = o.toString();
  try {
   double dou = Double.parseDouble(s);
  } catch (Exception e) {
   return "0";
  }
  return s;
 }

 public static double getDouble(Object o) {
  return Double.parseDouble(getNumberFormat(o));
 }

 public static long getLong(Object o) {
  return (long) getDouble(o);
 }

 public static int getInt(Object o) {
  return (int) getDouble(o);
 }

 public static float getFloat(Object o) {
  return (float) getDouble(o);
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 把一个字符串转换成字符串YYYYMMDD的形式
  * @param _date 传入的字串
  * @return 返回的字串
  */
 public static synchronized String getDateFormat(String _date) {
  if (_date == null) {
   return "";
  }
  if (_date.length() == 8) {
   return _date;
  }
  if (_date.length() > 8) {
   return _date.substring(0, 4) + _date.substring(5, 7)
     + _date.substring(8, 10);
  }
  return "";
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 把一个字符串转换成字符串YYYYMMDDHHMISS的形式
  * @param _date 传入的字串
  * @return 返回的字串
  */
 public static synchronized String getDateTimeFormat(String _date) {
  if (_date == null) {
   return "";
  }
  if (_date.length() == 8) {
   _date += "000000";
  }
  if (_date.length() == 14) {
   return _date;
  }
  if (_date.length() > 14) {
   return _date.substring(0, 4) + _date.substring(5, 7)
     + _date.substring(8, 10) + _date.substring(11, 13)
     + _date.substring(14, 16) + _date.substring(17, 19);
  }
  return "";
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 获取一个字符串的绝对长度(如果遇到汉字字符则算两个)
  * @param s 传入的字串
  * @return 字串的绝对长度
  */
 public static int absoluteLength(String s) {
  if (s == null) {
   return 0;
  }
  try {
   return new String(s.getBytes("GB2312"), "8859_1").length();
  } catch (Exception e) {
   return s.length();
  }
 }

 //------------------------------------------------------------------------------
 //
 /**
  * 对一个字符串的绝对长度进行拆解(如果遇到汉字字符会把它当作两个字符处理)
  * @param s 传入的字串
  * @param start 起始绝对位置
  * @param end 终止绝对位置
  * @return 返回的字串
  */
 public static String absoluteSubstring(String s, int start, int end) {
  if (s == null) {
   return null;
  }
  try {
   String s2 = new String(s.getBytes("GB2312"), "8859_1");
   s2 = s2.substring(start, end);
   return new String(s2.getBytes("8859_1"), "GB2312");
  } catch (Exception e) {
   return s.substring(start, end);
  }
 }

 /**
  * 在一个字符串两边加上"-"号
  * @param s 传入的字串
  * @param num 加上"-"号的个数
  * @return 返回的字串
  */
 public static String addLine(String s, int num) {
  for (int i = 0; i < num; i++) {
   s = "-" + s + "-";
  }
  return s;
 }

 /**
  * 扩充一个字串,使其绝对长度为指定的长度,如果过长就截断,过短就补充指定的字串
  * @param str 传入的字串
  * @param updateStr 填充的字串
  * @param num 指定的长度
  * @param flag 填补字符串的位置:true的话在前面填补、false在后面填补
  * @return 返回的字串
  */
 public static String updateAbsoluteLength(String str, String updateStr,int num, boolean flag) {
  if (updateStr == null) {
   return str;
  }
  if (str == null) {
   str = "";
   for (int i = 0; i < num; i++) {
    str += updateStr;
   }
   return str;
  }
  if (absoluteLength(str) == num) {
   return str;
  } else if (absoluteLength(str) < num) {
   for (int i = absoluteLength(str); i < num; i++) {
    if (flag) {
     str = updateStr + str;
    } else {
     str = str + updateStr;
    }
   }
   return str;
  } else {
   return absoluteSubstring(str, 0, absoluteLength(str) - num);
  }
 }

 public static String updateAbsoluteLength(String str, String updateStr,int num) {
  return updateAbsoluteLength(str, updateStr, num, true);
 }

 //------------------------------------------------------------------------------
 public static void main(String[] args) {
  //String c = new String("explorerwg");
  //String c2 = new String("anling");
  //String c3 = new String("administrator");
  //System.out.println(StringFunction.updateAbsoluteLength(c, "&&nbsp;", 28));
  //  System.out.println(StringFunction.updateAbsoluteLength(c2, "&&nbsp;", 28));
  //  System.out.println(StringFunction.updateAbsoluteLength(c3, "&&nbsp;", 28));
  System.out.println(like("222223322", "33"));
 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值