import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* @version V1.0
* @ClassName StringUtil
* @Description 字符串解析类
* @Author Alan
* @Date 2018/12/27 19:11
*/
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 objToString(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();
} /**
* 根据某个字符对字符串进行分割
* @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字符串操作工具类的更多相关文章

  1. docker 部署vsftpd服务、验证及java ftp操作工具类

    docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...

  2. Java字符串工具类

    import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.lang.re ...

  3. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

  4. 自用java字符串工具类

    不断封装一些常用的字符串操作加到这个工具类里,不断积累: package com.netease.lede.qa.util; import java.text.ParseException; impo ...

  5. Java日期操作工具类

    /** * 格式化日期显示格式 * * @param sdate * 原始日期格式 s - 表示 "yyyy-mm-dd" 形式的日期的 String 对象 * @param fo ...

  6. Java字符串String类操作方法详细整理

    关于String类的基本操作,可分为以下几类: 1.基本操作方法 2.字符串比较 3.字符串与其他数据类型之间的转换 4.字符与字符串的查找 5.字符串的截取与拆分 6.字符串的替换与修改 我觉得在整 ...

  7. java HttpClient操作工具类

    maven: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> ...

  8. Java文件操作工具类

    import com.foriseland.fjf.lang.DateUtil;import org.apache.commons.io.FileUtils;import org.slf4j.Logg ...

  9. Java文件操作工具类(复制、删除、重命名、创建路径)

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

随机推荐

  1. kafka window环境下使用(内置zookeeper)

    下载 kafka 官网下载最新版本(已集成 zookeeper) 解压到 D 盘的 kafka_2.12-2.3.0 运行 zookeeper 执行 zookeeper 运行命令 D:\kafka_2 ...

  2. MQTT研究之EMQ:【EMQX使用中的一些问题记录(3)】

    EMQX功能强大,但是帮助信息或者可用资料的确有限,遇到个问题,比较难找到处理的头绪,今天,我要记录的是,使用中出现EMQX宕机,但是呢,启动也启动不了. 今天记录的内容,就以操作EMQX 3.2.3 ...

  3. CentOS7下NFS服务安装及配置固定端口

    CentOS7下NFS服务安装及配置 系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:nfs-utils-1. ...

  4. PHP打印日志类

    PHP简单封装个打印日志类,方便查看日志: <?php /** * Created by PhpStorm. * User: zenkilan * Date: 2019/9/26 * Time: ...

  5. CentOS 7 卸载OpenJdk安装Oracle Jdk1.8

    CentOS 7 卸载OpenJdk安装Oracle Jdk1.81.查询openjdk:rpm -qa | grep jdk2.卸载OpenJdkrpm -e --nodeps 查询到的结果3.安装 ...

  6. JS 生成随机字符串 随机颜色

    使用Math.random()生成随机数 0.7489584611780002数字的.toString(n) 将数字转换为 n 进制的字符串 n取值范围(0~36)"0.vbpjw8lipf ...

  7. OSI七层模型、TCP/IP五层模型

    OSI网络互连的七层框架:物理层.数据链路层.网络层.传输层.会话层.表示层.应用层: <1>应用层 OSI参考模型中最靠近用户的一层,是为计算机用户提供应用接口,为用户直接提供各种网络服 ...

  8. 基于传统方法点云分割以及PCL中分割模块

      之前在微信公众号中更新了以下几个章节 1,如何学习PCL以及一些基础的知识 2,PCL中IO口以及common模块的介绍 3,PCL中常用的两种数据结构KDtree以及Octree树的介绍    ...

  9. 使用Python读写文件进行图片复制(文件复制)

    发现Python在读二进制文件时,可以生成一个新的文件,操作还很简单,如下:对一个jpeg的文件进行复制 fp1=open("e:\\1.jpeg","rb") ...

  10. php 回调函数结合闭包(匿名函数)的使用示例

    <?php /** * php 回调函数结合闭包(匿名函数)的使用 */ function callback( $callback ){ $variable = 'program'; $ret1 ...