Java字符串工具类
import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.lang.reflect.Method;import java.net.URLDecoder;import java.net.URLEncoder;import java.util.*;import java.util.regex.Matcher;import java.util.regex.Pattern;
/*** @ClassName StringUtil* @Description 字符串解析类* @Author Alan* @Date 2017/12/27 19:11* @version V1.0*/public class StringUtil {
/** * 创建指定数量的随机字符串 * * @param numberFlag * 是否是数字 * @param length * @return */ public static String createRandom(boolean numberFlag, int length) { String retStr = ""; String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz"; int len = strTable.length(); boolean bDone = true; do { retStr = ""; int count = 0; for (int i = 0; i < length; i++) { double dblR = Math.random() * len; int intR = (int) Math.floor(dblR); char c = strTable.charAt(intR); if (('0' <= c) && (c <= '9')) { count++; } retStr += strTable.charAt(intR); } if (count >= 2) { bDone = false; } } while (bDone);
return retStr; }
/** * 字节数组转换为字符串 * @return */ public static String byteToStr(byte[] byt) throws UnsupportedEncodingException { String strRead = new String(byt,"UTF-8"); return strRead; }
/** * 将字节数组转换为十六进制字符串 * @param src * @return * @throws UnsupportedEncodingException */ public static String bytes2Hex(byte[] src) throws UnsupportedEncodingException { if (src == null || src.length <= 0) { return null; }
char[] res = new char[src.length * 2]; // 每个byte对应两个字符 final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; for (int i = 0, j = 0; i < src.length; i++) { res[j++] = hexDigits[src[i] >> 4 & 0x0f]; // 先存byte的高4位 res[j++] = hexDigits[src[i] & 0x0f]; // 再存byte的低4位 }
return decode(new String(res),"UTF-8"); }
/** * 将16进制数字解码成字符串,适用于所有字符(包括中文) */ public static String decode(String bytes, String charset) throws UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length()/2); final String hexString="0123456789abcdef"; //将每2位16进制整数组装成一个字节 for(int i=0;i<bytes.length();i+=2) baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)))); return new String(baos.toByteArray(), charset); } /** * 字符串转换为字节数组 * @param str * @return */ public static byte[] strToByte(String str){ byte[] byBuffer = new byte[200]; String strInput=str; byBuffer= strInput.getBytes(); return byBuffer; } /** * 将URL编码转化为字符串 * @param str * @return * @throws UnsupportedEncodingException */ public static String strToDecoder(String str) { try { return URLDecoder.decode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return str; } } /** * 将字符串转化为URL编码 * @param str * @return * @throws UnsupportedEncodingException */ public static String strToEncoder(String str) throws UnsupportedEncodingException { return URLEncoder.encode(str, "UTF-8"); } /** * 去掉字符串中的空格、回车、换行符、制表符 * @param str * @return */ public static String replaceBlank(String str) { String dest = ""; if (str!=null) { Pattern p = Pattern.compile("\\s*|\t|\r|\n|"); Matcher m = p.matcher(str); dest = m.replaceAll(""); // 将ASCII的值为160的替换为空字符串 dest = dest.replace(backStr(160), ""); } return dest; } /** * 去掉字符串中的空格、回车、换行符、制表符、斜杠、点、冒号 * @param str * @return */ public static String replaceBlanks(String str){ str = replaceBlank(str); String dest = ""; if (str!=null) { Pattern p = Pattern.compile("/|\\.|\\:|"); Matcher m = p.matcher(str); dest = m.replaceAll(""); } return dest; } /** * 字符转ASC * * @param st * @return */ public static int getAsc(String st) { byte[] gc = st.getBytes(); int ascNum = (int) gc[0]; return ascNum; } /** * ASC转字符 * * @param backnum * @return */ public static char backchar(int backnum) { char strChar = (char) backnum; return strChar; } /** * ASC转字符串 * @param */ public static String backStr(int backnum) { char strChar = (char) backnum; return String.valueOf(strChar); } /** * 将Object转换为String * @param o * @return */ public static String isNull(Object o){ if (o==null) { return ""; } return o.toString(); } /** * 判断对象是否为空 * @param o * @return */ public static boolean isEmpty(Object o){ boolean result = false; if(o == null){ result = true; } else { if("".equals(o.toString())){ result = true; } } return result; } /*** * encode by Base64 */ public static String encodeBase64(byte[]input) throws Exception{ Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); Method mainMethod= clazz.getMethod("encode", byte[].class); mainMethod.setAccessible(true); Object retObj=mainMethod.invoke(null, new Object[]{input}); return (String)retObj; } /*** * decode by Base64 */ public static byte[] decodeBase64(String input) throws Exception{ Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); Method mainMethod= clazz.getMethod("decode", String.class); mainMethod.setAccessible(true); Object retObj=mainMethod.invoke(null, input); return (byte[])retObj; } public static byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } //将指定byte数组以16进制的形式打印到控制台 public static void printHexString( byte[] b) { for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } System.out.print(hex.toUpperCase() ); } } /** * 将list集合转化为like查询语句 * @param names * @return */ public static String listToLikeString (List<String> names) { StringBuffer sb = new StringBuffer(); sb.append("("); for(int i=0; i<names.size(); i++){ sb.append("'"); sb.append(names.get(i)); sb.append("'"); if(i != names.size() - 1){ sb.append(","); } } sb.append(")"); return sb.toString(); } /** * 对Json字符串进行排序 * @param str * @return */ public static String sortString (String str) { try { Map<Object, Object> map = FastJsonUtil.getMapJSON(str); TreeMap treeMap = new TreeMap(map); return FastJsonUtil.parseToJSON(treeMap); } catch (Exception e) { return str; } } /** * 根据某个字符对字符串进行分割 * @param str * @return */ public static String[] spilt(String str, String reg){ str = replaceBlank(str); String[] spilt = str.split(reg); return spilt; } /** * 根据逗号进行分割 * @param str * @return */ public static String[] spilt(String str){ return spilt(str,","); } public static void main(String[] args) { String s = "http://192.168.2.26:4000/"; s = replaceBlanks(s); System.out.println(s); }}
Java字符串工具类的更多相关文章
- 自用java字符串工具类
不断封装一些常用的字符串操作加到这个工具类里,不断积累: package com.netease.lede.qa.util; import java.text.ParseException; impo ...
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- StringUtils 字符串工具类
package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import j ...
- Java json工具类,jackson工具类,ObjectMapper工具类
Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...
- JAVA String 工具类
java StringUtil 字符串工具类 import java.util.ArrayList; import java.util.LinkedHashSet; import java.util. ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
- Jsoup请求http或https返回json字符串工具类
Jsoup请求http或https返回json字符串工具类 所需要的jar包如下: jsoup-1.8.1.jar 依赖jar包如下: httpclient-4.5.4.jar; httpclient ...
- java日期工具类DateUtil-续一
上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...
- java日期工具类DateUtil-续二
该版本是一次较大的升级,农历相比公历复杂太多(真佩服古人的智慧),虽然有规律,但涉及到的取舍.近似的感念太多,况且本身的概念就已经很多了,我在网上也是查阅了很多的资料,虽然找到一些计算的方法,但都有些 ...
随机推荐
- 前端中用到的图片(png图片)
作为前端工程师,将设计师的设计稿转化为html页面,其中有一个必不可少的环节就是将psd文件中的一些图片随心所欲的使用,而我们经常用到的就是png图片. 第一部分:基本介绍 首先我们先对比几种图片: ...
- Robot Framework自动化测试一(第一个脚本)
创建测试项目 选择菜单栏file----->new Project Name 输入项目名称,Type 选择Di ...
- Apache和Tomcat的整合过程(转载)
一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...
- springmvc的json数据交互
准备 @RequestBody 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容(json ...
- TOJ 1836 Play on Words
描述 Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has t ...
- CCF ISBN号码 201312-2
ISBN号码 问题描述 试题编号: 201312-2 试题名称: ISBN号码 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 每一本正式出版的图书都有一个ISBN号码与之对应 ...
- 关于React Hooks,你不得不知的事
React Hooks是React 16.8发布以来最吸引人的特性之一.在开始介绍React Hooks之前,让咱们先来理解一下什么是hooks.wikipedia是这样给hook下定义的: In c ...
- disruptor--Introduction
The best way to understand what the Disruptor is, is to compare it to something well understood and ...
- .netCore2.0 WebApi 传递form表单
随着it的技术发展,目前越来越多的项目采用前后端分离的开发模式,通过webapi提供接口数据来进行交互 最近项目用的是.netCore WebApi,在最近的项目使用中发现一些问题,进行记录.个人简介 ...
- 阿里云、青云、腾讯云服务器,Mysql数据库,Redis等产品性能对比
阿里云.青云.腾讯云服务器,Mysql数据库,Redis等产品都使用过,对比维度很多就不一一放出.直接放结论吧:买的腾讯(金融专区)服务器,Mysql(TDSql)把所有项目转到腾讯云,但是没有用腾讯 ...