操作字符串的工具类

 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class StringUtil { /**
* 过滤空NULL
* @param o
* @return
*/
public static String FilterNull(Object o) {
return o != null && !"null".equals(o.toString()) ? o.toString().trim() : "" ;
} /**
* 是否为空
* @param o
* @return
*/
public static boolean isEmpty(Object o) {
if (o == null) {
return true;
}
if ("".equals(FilterNull(o.toString()))) {
return true;
} else {
return false;
}
} /**
* 是否不为空
* @param o
* @return
*/
public static boolean isNotEmpty(Object o) {
if (o == null) {
return false;
}
if ("".equals(FilterNull(o.toString()))) {
return false;
} else {
return true;
}
} /**
* 是否可转化为数字
* @param o
* @return
*/
public static boolean isNum(Object o) {
try {
new BigDecimal(o.toString());
return true;
} catch (Exception e) {
}
return false;
} /**
* 是否可转化为Long型数字
* @param o
* @return
*/
public static boolean isLong(Object o) {
try {
new Long(o.toString());
return true;
} catch (Exception e) {
}
return false;
} /**
* 转化为Long型数字, 不可转化时返回0
* @param o
* @return
*/
public static Long toLong(Object o) {
if (isLong(o)) {
return new Long(o.toString());
} else {
return 0L;
}
} /**
* 转化为int型数字, 不可转化时返回0
* @param o
* @return
*/
public static int toInt(Object o) {
if (isNum(o)) {
return new Integer(o.toString());
} else {
return 0;
}
} /**
* 按字符从左截取固定长度字符串, 防止字符串超长, 默认截取50
* @param o
* @return
*/
public static String holdmaxlength(Object o) {
int maxlength = 50;
if (o == null) {
return "";
}
return subStringByByte(o, maxlength);
} /**
* 从左截取固定长度字符串, 防止字符串超长, maxlength为0时默认50
* @param o
* @return
*/
public static String holdmaxlength(Object o, int maxlength) {
maxlength = maxlength <= 0 ? 50 : maxlength;
if (o == null) {
return "";
}
return subStringByByte(o, maxlength);
} /**
* 按字节截取字符串
* @param str
* @param len
* @return
*/
private static String subStringByByte(Object o, int len) {
if (o == null) {
return "";
}
String str = o.toString();
String result = null;
if (str != null) {
byte[] a = str.getBytes();
if (a.length <= len) {
result = str;
} else if (len > 0) {
result = new String(a, 0, len);
int length = result.length();
if (str.charAt(length - 1) != result.charAt(length - 1)) {
if (length < 2) {
result = null;
} else {
result = result.substring(0, length - 1);
}
}
}
}
return result;
} /**
* 逗号表达式_添加
* @param commaexpress 原逗号表达式 如 A,B
* @param newelement 新增元素 C
* @return A,B,C
*/
public static String comma_add(String commaexpress, String newelement) {
return comma_rect(FilterNull(commaexpress) + "," + FilterNull(newelement));
} /**
* 逗号表达式_删除
* @param commaexpress 原逗号表达式 如 A,B,C
* @param delelement 删除元素 C,A
* @return B
*/
public static String comma_del(String commaexpress, String delelement) {
if ((commaexpress == null) || (delelement == null) || (commaexpress.trim().equals(delelement.trim()))) {
return "";
}
String[] deletelist = delelement.split(",");
String result = commaexpress;
for (String delstr : deletelist) {
result = comma_delone(result, delstr);
}
return result;
} /**
* 逗号表达式_单一删除
* @param commaexpress 原逗号表达式 如 A,B,C
* @param delelement 删除元素 C
* @return A,B
*/
public static String comma_delone(String commaexpress, String delelement) {
if ((commaexpress == null) || (delelement == null) || (commaexpress.trim().equals(delelement.trim()))) {
return "";
}
String[] strlist = commaexpress.split(",");
StringBuffer result = new StringBuffer();
for (String str : strlist) {
if ((!str.trim().equals(delelement.trim())) && (!"".equals(str.trim()))) {
result.append(str.trim() + ",");
}
}
return result.toString().substring(0, result.length() - 1 > 0 ? result.length() - 1 : 0);
} /**
* 逗号表达式_判断是否包含元素
* @param commaexpress 逗号表达式 A,B,C
* @param element C
* @return true
*/
public static boolean comma_contains(String commaexpress, String element) {
boolean flag = false;
commaexpress = FilterNull(commaexpress);
element = FilterNull(element);
if (!"".equals(commaexpress) && !"".equals(element)) {
String[] strlist = commaexpress.split(",");
for (String str : strlist) {
if (str.trim().equals(element.trim())) {
flag = true;
break;
}
}
}
return flag;
} /**
* 逗号表达式_取交集
* @param commaexpressA 逗号表达式1 A,B,C
* @param commaexpressB 逗号表达式2 B,C,D
* @return B,C
*/
public static String comma_intersect(String commaexpressA, String commaexpressB) {
commaexpressA = FilterNull(commaexpressA);
commaexpressB = FilterNull(commaexpressB);
StringBuffer result = new StringBuffer();
String[] strlistA = commaexpressA.split(",");
String[] strlistB = commaexpressB.split(",");
for (String boA : strlistA) {
for (String boB : strlistB) {
if (boA.trim().equals(boB.trim())) {
result.append(boA.trim() + ",");
}
}
}
return comma_rect(result.toString());
} /**
* 逗号表达式_规范
* @param commaexpress 逗号表达式 ,A,B,B,,C
* @return A,B,C
*/
public static String comma_rect(String commaexpress) {
commaexpress = FilterNull(commaexpress);
String[] strlist = commaexpress.split(",");
StringBuffer result = new StringBuffer();
for (String str : strlist) {
if (!("".equals(str.trim())) && !("," + result.toString() + ",").contains("," + str + ",") && !"null".equals(str)) {
result.append(str.trim() + ",");
}
}
return result.toString().substring(0, (result.length() - 1 > 0) ? result.length() - 1 : 0);
} /**
* 逗号表达式_反转
* @param commaexpress A,B,C
* @return C,B,A
*/
public static String comma_reverse(String commaexpress) {
commaexpress = FilterNull(commaexpress);
String[] ids = commaexpress.split(",");
StringBuffer str = new StringBuffer();
for (int i = ids.length - 1; i >= 0; i--) {
str.append(ids[i] + ",");
}
return comma_rect(str.toString());
} /**
* 逗号表达式_获取首对象
* @param commaexpress A,B,C
* @return A
*/
public static String comma_first(String commaexpress) {
commaexpress = FilterNull(commaexpress);
String[] ids = commaexpress.split(",");
System.out.println("length:" + ids.length);
if ((ids != null) && (ids.length > 0)) {
return ids[0];
}
return null;
} /**
* 逗号表达式_获取尾对象
* @param commaexpress A,B,C
* @return C
*/
public static String comma_last(String commaexpress) {
commaexpress = FilterNull(commaexpress);
String[] ids = commaexpress.split(",");
if ((ids != null) && (ids.length > 0)) {
return ids[(ids.length - 1)];
}
return null;
} /**
* 替换字符串,支持字符串为空的情形
* @param strData
* @param regex
* @param replacement
* @return
*/
public static String replace(String strData, String regex, String replacement) {
return strData == null ? "" : strData.replaceAll(regex, replacement);
} /**
* 字符串转为HTML显示字符
* @param strData
* @return
*/
public static String String2HTML(String strData){
if( strData == null || "".equals(strData) ){
return "" ;
}
strData = replace(strData, "&", "&amp;");
strData = replace(strData, "<", "&lt;");
strData = replace(strData, ">", "&gt;");
strData = replace(strData, "\"", "&quot;");
return strData;
} /** * 把异常信息转换成字符串,以方便保存 */
public static String getexceptionInfo(Exception e){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
e.printStackTrace(new PrintStream(baos));
}finally{
try {
baos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return baos.toString();
} /** 过滤特殊符号 */
public static String regex(String str){
Pattern pattern = Pattern.compile("[0-9-:/ ]");// 中文汉字编码区间
Matcher matcher;
char[] array = str.toCharArray();
for (int i = 0; i < array.length; i++) {
matcher = pattern.matcher(String.valueOf(array[i]));
if (!matcher.matches()) {// 空格暂不替换
str = str.replace(String.valueOf(array[i]), "");// 特殊字符用空字符串替换
}
} return str;
} public static String comma_insert(String commaexpress, String newelement,int index){
int length = commaexpress.length();
if ( index > length ) {
index = length;
}else if ( index < 0){
index = 0;
}
String result = commaexpress.substring(0, index) + newelement + commaexpress.substring(index, commaexpress.length());
return result;
} /**
* 将"/"替换成"\"
* @param strDir
* @return
*/
public static String changeDirection(String strDir) {
String s = "/";
String a = "\\";
if (strDir != null && !" ".equals(strDir)) {
if (strDir.contains(s)) {
strDir = strDir.replace(s, a);
}
}
return strDir;
} /**
* 去除字符串中 头和尾的空格,中间的空格保留
*
* @Title: trim
* @Description: TODO
* @return String
* @throws
*/
public static String trim(String s) {
int i = s.length();// 字符串最后一个字符的位置
int j = 0;// 字符串第一个字符
int k = 0;// 中间变量
char[] arrayOfChar = s.toCharArray();// 将字符串转换成字符数组
while ((j < i) && (arrayOfChar[(k + j)] <= ' '))
++j;// 确定字符串前面的空格数
while ((j < i) && (arrayOfChar[(k + i - 1)] <= ' '))
--i;// 确定字符串后面的空格数
return (((j > 0) || (i < s.length())) ? s.substring(j, i) : s);// 返回去除空格后的字符串
}
/**
* 得到大括号中的内容
* @param str
* @return
*/
public static String getBrackets(String str) {
int a = str.indexOf("{");
int c = str.indexOf("}");
if (a >= 0 && c >= 0 & c > a) {
return (str.substring(a + 1, c));
} else {
return str;
}
} /**
* 将字符串中所有的,替换成|
*
* @param str
* @return
*/
public static String commaToVerti(String str) {
if (str != null && !"".equals(str) && str.contains(",")) {
return str.replaceAll(",", "|");
} else {
return str;
}
} /**
* 去掉字符串中、前、后的空格
* @param args
* @throws IOException
*/
public static String extractBlank(String name) {
if (name != null && !"".equals(name)) {
return name.replaceAll(" +", "");
} else {
return name;
}
} /**
* 将null换成""
* @param str
* @return
*/
public static String ConvertStr(String str) {
return str != null && !"null".equals(str) ? str.trim() : "";
} public static void main(String[] args){
System.out.println(isNum("a"));
System.out.println(isNum("-1"));
System.out.println(isNum("01"));
System.out.println(isNum("1E3"));
System.out.println(isNum("1.a"));
System.out.println(isLong("014650"));
System.out.println(Long.parseLong("014650"));
}
}

Java操作字符串的工具类的更多相关文章

  1. 最全的Java操作Redis的工具类,使用StringRedisTemplate实现,封装了对Redis五种基本类型的各种操作!

    转载自:https://github.com/whvcse/RedisUtil 代码 ProtoStuffSerializerUtil.java import java.io.ByteArrayInp ...

  2. java 二进制数字符串转换工具类

    java 二进制数字符串转换工具类 将二进制转换成八进制 将二进制转换成十进制 将二进制转换成十六进制 将十进制转换成二进制 package com.iteye.injavawetrust.ad; i ...

  3. Java操作图片的工具类

    操作图片的工具类: import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.a ...

  4. Java 操作jar包工具类以及如何快速修改Jar包里的文件内容

    需求背景:写了一个实时读取日志文件以及监控的小程序,打包成了Jar包可执行文件,通过我们的web主系统上传到各个服务器,然后调用ssh命令执行.每次上传前都要通过解压缩软件修改或者替换里面的配置文件, ...

  5. java操作数组的工具类-Arrays

    static int binarySearch(type[] a, type key) 使用二分搜索法来搜索key元素在数组中的索引:若a数组不包括key,返回负数.(该方法必须已按升序排列后调用). ...

  6. Java操作XML的工具类

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Inp ...

  7. Java随机字符串:随机数字字符串,工具类

    Java中生成随机数,字符串的工具类 1. 调用方法需要传入生成字符串的长度和需要的类型 生成随机数字 生成随机字母字符串 生成随机字符串+数字等 ......... 2. 总共8种类型,具体看工具类 ...

  8. java后端时间处理工具类,返回 "XXX 前" 的字符串

    转自:https://www.cnblogs.com/devise/p/9974672.html 我们经常会遇到显示 "某个之间之前" 的需求(比如各种社交软件,在回复消息时,显示 ...

  9. java里poi操作excel的工具类(兼容各版本)

    转: java里poi操作excel的工具类(兼容各版本) 下面是文件内具体内容,文件下载: import java.io.FileNotFoundException; import java.io. ...

随机推荐

  1. work_5

    第五次作业对我个人来说是很难的,因为之前没怎么接触过这方面的内容,有幸能跟宗毅组成一队,我也仔细看了他的Python代码,因为对于Python也是第一次接触,所以我感觉在有限的时间里学会并且灵活运用还 ...

  2. Java设计模式系列之中介者模式

    中介者模式(Mediator)的定义 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式(Mediator)的适 ...

  3. Spring EL hello world example

    The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...

  4. HDU 5773 The All-purpose Zero (变形LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 0可以改变成任何数,问你严格递增的子序列最长是多少. 猜测0一定在最长上升子序列中用到,比如2 ...

  5. UVa 817 According to Bartjens (暴力,DFS)

    题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子. 所有数字不能有前导0, 且式子必须是合法的. 析:这个题很明显的暴 ...

  6. cocos2dx搭建开发环境

    windows7 64位 搭建cocos2dx 版本开发环境 目前cocos2dx分为2.x版本和3.x版本,搭建环境稍有不同 先搭建3.1版本win32开发环境 相关准备: 注意:安装路径尽可能不要 ...

  7. C#学习笔记(六):可空类型、匿名方法和迭代器

    可空类型 为啥要引入可空类型? 在数据库中,字段是可以为null值的,那么在C#中为了方便的操作数据库的值,微软引入了可空类型. 声明可空类型 我们可以使用两种方法声明一个可空类型: Nullable ...

  8. Nexus搭建Manven

    Nexus相当于中转服务器,减轻网络的负载,加速项目搭建的进程 1.下载地址:http://www.sonatype.org/nexus/go 2.下载的是zip包,解压后进入D:\nexus-2.8 ...

  9. Linux下通过JDBC连接Oracle,SqlServer和PostgreSQL

    今天正好需要统计三个网站栏目信息更新情况,而这三个网站的后台采用了不同的数据库管理系统.初步想法是通过建立一个小的Tomcat webapp,进而通过JDBC访问这三个后台数据库,并根据返回的数据生成 ...

  10. Android下结束进程的方法

    转自:http://www.cnblogs.com/crazypebble/archive/2011/04/05/2006213.html 最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这 ...