Java 工具包

package com;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class StringUntil {
	/**
	 * 分割字符串
	 * 
	 * @param str String 原始字符串
	 * @param splitsign String 分隔符
	 * @return String[] 分割后的字符串数组
	 */
	@SuppressWarnings("unchecked")
	public static String[] split(String str, String splitsign) {
		int index;
		if (str == null || splitsign == null)
			return null;
		ArrayList al = new ArrayList();
		while ((index = str.indexOf(splitsign)) != -1) {
			al.add(str.substring(0, index));
			str = str.substring(index + splitsign.length());
		}
		al.add(str);
		return (String[]) al.toArray(new String[0]);
	}

	/**
	 * 
	 * 替换字符串
	 * 
	 * @param from String 原始字符串
	 * @param to String 目标字符串
	 * @param source String 母字符串
	 * @return String 替换后的字符串
	 */

	public static String replace(String from, String to, String source) {

		if (source == null || from == null || to == null)
			return null;
		StringBuffer bf = new StringBuffer("");
		int index = -1;
		while ((index = source.indexOf(from)) != -1) {
			bf.append(source.substring(0, index) + to);
			source = source.substring(index + from.length());
			index = source.indexOf(from);
		}
		bf.append(source);
		return bf.toString();

	}

	/**
	 * 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
	 * 
	 * @param str  String 原始字符串
	 * @return String 替换后的字符串
	 */

	public static String htmlencode(String str) {

		if (str == null) {
			return null;
		}
		return replace("\"", """, replace("<", str="" str="" string="" string="" public="" static="" string="" htmldecode="" str="" if="" null="" return="" null="" return="" replace="" replace="" str="" private="" static="" final="" string="" _br="'
0) { c = str.charAt(index); if (c < 128) { length--; } else { length--; length--; } buff.append(c); index++; } buff.append("..."); return buff.toString(); } /** * 判断是否为整数 * @param str 传入的字符串 * @return 是整数返回true,否则返回false */ public static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } /** * 判断是否为浮点数,包括double和float * @param str 传入的字符串 * @return 是浮点数返回true,否则返回false */ public static boolean isDouble(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$"); return pattern.matcher(str).matches(); } /** * 判断输入的字符串是否符合Email样式. * @param str 传入的字符串 * * @return 是Email样式返回true,否则返回false */ public static boolean isEmail(String str) { Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); return pattern.matcher(str).matches(); } /** * 判断输入的字符串是否为纯汉字 * @param str 传入的字符串 * @return 如果是纯汉字返回true,否则返回false */ public static boolean isChinese(String str) { Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$"); return pattern.matcher(str).matches(); } /** * 是否为空白,包括null和"" * * @param str * @return */ public static boolean isBlank(String str) { return str == null || str.trim().length() == 0; } /** * 判断是不是合法手机 handset 手机号码 */ public static boolean isHandset(String handset) { try { if (!handset.substring(0, 1).equals("1")) { return false; } if (handset == null || handset.length() != 11) { return false; } String check = "^[0123456789]+$"; Pattern regex = Pattern.compile(check); Matcher matcher = regex.matcher(handset); boolean isMatched = matcher.matches(); if (isMatched) { return true; } else { return false; } } catch (RuntimeException e) { return false; } } /** * 数组转为字符串 * * @param String[] s 数组 * @param Stringcompart 分隔符 **/ public static String ArrayToString(String[] s, String compart) { int fieldsNumber = s.length; String field = ""; for (int i = 0; i < fieldsNumber; i++) { if ((i + 1) != fieldsNumber) { field += " " + compart + s[i] + compart + ","; } else { field += " " + compart + s[i] + compart; } } return field; } // ISO编码转换成GBK编码 public static String convertGBK(String str) { try { if (isEmpty(str)) return ""; byte[] bytesStr = str.getBytes("ISO-8859-1"); return new String(bytesStr, "gb2312"); } catch (Exception ex) { return str; } } // GBK编码转换成ISO编码 public static String convertISO(String str) { try { if (isEmpty(str)) return ""; byte[] bytesStr = str.getBytes("gb2312"); return new String(bytesStr, "ISO-8859-1"); } catch (Exception ex) { return str; } } /** * 判断字符串是否为空 */ public static boolean isEmpty(String str) { boolean flag = false; if (str == null || "".equals(str) || "null".equals(str)) { flag = true; } else { flag = false; } return flag; } /** * 判断字符串是否为空 * * @return New String 空值而不是null */ public static String isNewStr(String str) { String flag = str; if (str == null || str.equals("") || str.equals("null")) flag = ""; return flag; } /** * YYYY-MM-DD **/ public static String getNowDate() { Calendar cal = Calendar.getInstance(); int intMonth = cal.get(cal.MONTH) + 1; int intDate = cal.get(cal.DATE); String sMonth = String.valueOf(intMonth); String sDate = String.valueOf(intDate); if (intMonth < 10) { sMonth = "0" + sMonth; } if (intDate < 10) { sDate = "0" + sDate; } return cal.get(cal.YEAR) + "-" + sMonth + "-" + sDate; } public static String getProperties(String path, String propertiesName) { Properties prop = new Properties(); String name = ""; try { FileInputStream in = new FileInputStream(path); prop.load(in); in.close(); name = prop.getProperty(propertiesName); prop.clear(); } catch (Exception e) { e.printStackTrace(); } return name; } /** * 按照字节截取字符串长度 * * @param s * @param length * @return * @throws Exception */ public static String bSubstring(String s, int length) throws Exception { byte[] bytes = s.getBytes("Unicode"); int n = 0; // 表示当前的字节数 int i = 2; // 要截取的字节数,从第3个字节开始 for (; i < bytes.length && n < length; i++) { // 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节 if (i % 2 == 1) { n++; // 在UCS2第二个字节时n加1 } else { // 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节 if (bytes[i] != 0) { n++; } } } // 如果i为奇数时,处理成偶数 if (i % 2 == 1) { // 该UCS2字符是汉字时,去掉这个截一半的汉字 if (bytes[i - 1] != 0) i = i - 1; // 该UCS2字符是字母或数字,则保留该字符 else i = i + 1; } return new String(bytes, 0, i, "Unicode"); } public static String clearHtml(String str) { int flag1 = str.indexOf("<"); int="" flag2="str.indexOf('>');" while="" -1="" flag2="" -1="" str="clearHtmlPrivate(str);" flag1="str.indexOf('<');" flag2="str.indexOf('>');" return="" str="" private="" static="" string="" clearhtmlprivate="" str="" int="" start="str.indexOf('<');" int="" end="str.indexOf('>');" if="" -1="" end="" -1="" str="str.substring(0," start="" str="" 1="" return="" str="" public="" static="" string="" superclearhtml="" str="" str="str.replaceAll('
.*|'," replaceall="" return="" str="" public="" static="" string="" escape="" src="" int="" i="" char="" j="" stringbuffer="" tmp="new" stringbuffer="" tmp="" 6="" for="" 0="" i="" src="" i="" j="src.charAt(i);" if="" character="" character="" tmp="" else="" if="" 256="" tmp="" if="" 16="" tmp="" tmp="" 16="" else="" tmp="" tmp="" 16="" return="" tmp="" public="" static="" string="" unescape="" src="" if="" null="" return="" null="" stringbuffer="" tmp="new" stringbuffer="" tmp="" int="" lastpos="0," pos="0;" char="" ch="" while="" src="" pos="src.indexOf('%'," lastpos="" if="" lastpos="" if="" 1="" ch="(char)" integer="" 2="" pos="" 6="" 16="" tmp="" lastpos="pos" 6="" else="" if="" 1="" src="" 1="" tmp="" pos="" 1="" lastpos="pos" 1="" else="" ch="(char)" integer="" 1="" pos="" 3="" 16="" tmp="" lastpos="pos" 3="" else="" if="" -1="" tmp="" lastpos="src.length();" else="" tmp="" pos="" lastpos="pos;" return="" tmp="" yyyymmddhhnnssmi="" public="" static="" string="" getrandomnumber="" calendar="" cal="Calendar.getInstance();" int="" intmonth="cal.get(cal.MONTH)" 1="" int="" intdate="cal.get(cal.DATE);" int="" hh="cal.get(Calendar.HOUR);" int="" nn="cal.get(Calendar.MINUTE);" int="" ss="cal.get(Calendar.SECOND);" int="" mi="cal.get(Calendar.MILLISECOND);" string="" smonth="String.valueOf(intMonth);" string="" sdate="String.valueOf(intDate);" if="" 10="" smonth="0" smonth="" if="" 10="" sdate="0" sdate="" return="" cal="" smonth="" sdate="" hh="" nn="" ss="" mi="" public="" static="" string="" convertmarks="" args="" if="" if="" args="args.trim();" return="" args="" str="" public="" static="" string="" convertnull="" str="" boolean="" flag="false;" if="" null="" return="" else="" return="" str="" input="" public="" static="" string="" tosbc="" input="" char="" c="input.toCharArray();" for="" i="0;" i="" c="" i="" if="" 32="" c="" 12288="" continue="" if="" 127="" c="" 65248="" return="" new="" string="" input="" public="" static="" string="" todbc="" input="" char="" c="input.toCharArray();" for="" i="0;" i="" c="" i="" if="" 12288="" c="" 32="" continue="" if=""> 65280 && c[i] < 65375) c[i] = (char) (c[i] - 65248); } return new String(c); } public static void main(String[] args) { String str = "0.215"; StringUntil s = new StringUntil(); String aa = StringUntil.getRandomNumber(); System.out.println(aa); } } </");></",></",>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值