总结-处理字符串常用方法类
来源:互联网 发布:php的优点和缺点 编辑:程序博客网 时间:2024/05/20 04:49
WEB工程上都在操作String字符串数据
根据工程的需要,写了很多处理字符串的方法
现在总结了一下最常用的字符串处理
StringUtil.java
- package com.chyi.core.utils;
- import java.text.DecimalFormat;
- import java.util.List;
- import java.util.UUID;
- import java.util.regex.Pattern;
- /**
- * 描述:字符串处理类的常用方法
- * @author 齐继超
- *
- */
- public class StringUtil {
- /**
- * 描述:取得32位的通用标识UUID
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static String getUUID(){
- return UUID.randomUUID().toString().replaceAll("-", "");
- }
- /**
- * 描述:将没有实例化的字符串转换成空字符串
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static String convertNull(String str){
- if (str==null) {
- return "";
- }else {
- return str;
- }
- }
- /**
- * 描述:Object类转换为String,避免在Object为null时,直接toString()出错
- * @param obj
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String obj2Str(Object obj) {
- return obj == null ? null : obj.toString();
- }
- /**
- * 描述:判断为空,返回true
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static boolean isBlank(String str){
- if(null == str || "".equals(str.trim())){
- return true;
- }
- return false;
- }
- /**
- * 描述:接收一个list,如果有一个为空,返回true
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static boolean isBlank(List<String> str){
- for (String string : str) {
- if (isBlank(string)) {
- return true;
- }
- }
- return false;
- }
- /**
- * 描述:不为空,返回true
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static boolean isNotBlank(String str){
- return !isBlank(str);
- }
- /**
- * 描述:接收一个list,如果都不为空,返回true
- * @param list
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static boolean isNotBlank(List<String> list){
- return !isBlank(list);
- }
- /**
- * 描述:将textarea输入的文本,转换成HTML格式输出
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static String textareaToHtml(String text){
- if (text!=null) {
- return text.replaceAll("/r/n", "<br>").replaceAll(" ", " ");
- }else {
- return null;
- }
- }
- /**
- * 描述:过滤HTML标签
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String filterHtml(String str){
- if (str==null) {
- return "";
- }else {
- str = str.replaceAll("<", "<");
- str = str.replaceAll(">", ">");
- }
- return str;
- }
- /**
- * 描述:全数字判断
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static boolean isNumber(String strIn){
- return isNumber(strIn,"0123456789");
- }
- /**
- * 描述:全数字判断,strRef 参照字可以自定义
- * @param strIn
- * @param strRef 参照字
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static boolean isNumber(String strIn,String strRef){
- if (strIn== null || strIn.length()==0) {
- return false;
- }
- for (int i = 0; i < strIn.length(); i++) {
- String strTemp = strIn.substring(i, i+1);
- if (strRef.indexOf(strTemp, 0) == -1) {
- return false;
- }
- }
- return true;
- }
- /**
- * 描述:判断是否是汉字
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static boolean isHanzi(String str) {
- char[] chars=str.toCharArray();
- boolean isGB2312=false;
- for(int i=0;i<chars.length;i++){
- byte[] bytes=(""+chars[i]).getBytes();
- if(bytes.length==2){
- int[] ints=new int[2];
- ints[0]=bytes[0]& 0xff;
- ints[1]=bytes[1]& 0xff;
- if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){
- isGB2312=true;
- break;
- }
- }
- }
- return isGB2312;
- }
- /**
- * 描述:是否包含汉字
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static boolean isHasHanz(String str){
- int strLen = new String(str).length();
- return (str.getBytes().length == strLen)?false:true;
- }
- /**
- * 描述:判断是否为浮点型
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static boolean isFloat(String strIn){
- if (strIn== null || strIn.length()==0)
- return false ;
- Pattern pattern = Pattern.compile("[0-9]*(//.?)[0-9]*");
- return pattern.matcher(strIn).matches();
- }
- /**
- * 描述:电话号码格式化 例如13233333333 return 132****3333
- * @param tel
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String telReplaceXX(String tel) {
- if (org.apache.commons.lang.StringUtils.isBlank(tel)) {
- return null;
- }
- char[] strs = tel.toCharArray();
- for (int i = 3; i < 7; i++) {
- strs[i] = '*';
- }
- return new String(strs);
- }
- /**
- * 描述:阿拉伯数值转换成汉字
- * @param a
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String numToHanz(int a) {
- String[] units = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
- String[] nums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
- String result = "";
- if (a < 0) {
- result = "负";
- a = Math.abs(a);
- }
- String t = String.valueOf(a);
- for (int i = t.length() - 1; i >= 0; i--) {
- int r = (int) (a / Math.pow(10, i));
- if (r % 10 != 0) {
- String s = String.valueOf(r);
- String l = s.substring(s.length() - 1, s.length());
- result += nums[Integer.parseInt(l) - 1];
- result += (units[i]);
- } else {
- if (!result.endsWith("零")) {
- result += "零";
- }
- }
- }
- return result;
- }
- /**
- * 描述:字符串替换,
- * @param strObj
- * @param str1
- * @param str2
- * @return
- * 作者:齐继超
- * 时间:Nov 27, 2010
- */
- public static String replace(String strObj,String str1,String str2){
- if ("".equals(str1)){
- return strObj;
- }
- return org.apache.commons.lang.StringUtils.replace(strObj, str1, str2);
- }
- /**
- * 描述:根据内容截取摘要
- * @param str
- * @param subLen
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String subSummary(String str,int subLen){
- if (str==null)return null;
- String regexp="<.*?>";
- str = str.replace(regexp, "");
- int len = str.length();
- if (len > subLen) {
- return str.substring(0, subLen);
- }else {
- return str;
- }
- }
- /**
- * 描述:描述:截取字符串省略代替... 例如:输入"abcdefg",3 返回 "abc...".
- * @param str
- * @param len
- * @param isOmit 是否省略...
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String subString(String str,int len,boolean isOmit){
- if(str==null || str.length()<=len)
- return str;
- if (isOmit) {
- return str.substring(0, len)+"...";
- }else {
- return str.substring(0, len);
- }
- }
- /**
- * 描述:返回字符串的len个字符.取前后,去掉中间 例如:输入"abcdefg",3 返回 "ab ... g".
- * @param value
- * @param len
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String getLmtStrx(String value, int len) {
- if(value == null || value.length() <= len)
- return value;
- value = value.substring(0,len/2) + ".." + value.substring(value.length()-len/2);
- return value;
- }
- /**
- * 描述:字符串切割,比如"2010-11-29" 按照"-"
- * @param strObj
- * @param delimiter
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String[] split(String strObj, String delimiter) {
- if(strObj == null) {
- return null;
- }
- if("".equals(strObj) || "".equals(delimiter)) {
- return new String[] {
- strObj};
- }
- int count = org.apache.commons.lang.StringUtils.countMatches(strObj,
- delimiter) + 1; //调用org.apache.commons
- int length = delimiter.length();
- String[] strs = new String[count];
- int posPre = 0 - length;
- int pos = 0;
- for(int i = 0; i < count - 1; i++) {
- pos = strObj.indexOf(delimiter, posPre + length);
- strs[i] = strObj.substring(posPre + length, pos);
- posPre = pos;
- }
- strs[count - 1] = strObj.substring(posPre + length);
- return strs;
- }
- /**
- * 描述:给传入的字符串前补足'0',以使字符串长度为len。例如:输入字符串:"23",4 返回:"0023"。
- * @param str
- * @param len
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String zeroStr(String str,int len){
- int strLen= str.length();
- for (int i = 0; i < len-strLen; i++) {
- str = "0"+str;
- }
- return str;
- }
- /**
- * 描述:输入0000返回0001,输入00000001返回00000002
- * @param rank
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String createRank(String rank){
- String prefix = null;
- String suffix = null;
- if (rank.trim().length() > 4) {
- prefix = rank.substring(0, rank.length() - 4);
- suffix = rank.substring(prefix.length());
- long tmp = Long.parseLong(suffix);
- tmp++;
- int len = suffix.length();
- suffix = new Long(tmp).toString();
- len = len - suffix.length();
- for (int n = 0; n < len; n++) {
- suffix = "0" + suffix;
- }
- rank = prefix + suffix;
- } else {
- long tmp = Long.parseLong(rank);
- tmp++;
- int len = rank.length();
- rank = new Long(tmp).toString();
- len = len - rank.length();
- for (int n = 0; n < len; n++) {
- rank = "0" + rank;
- }
- }
- return rank;
- }
- /**
- * 描述:浮点格式化 例如value=45.678,format=000.00,返回值就是045.68
- * @param value
- * @param format
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String getFormatDouble(double value, String format) {
- DecimalFormat d = new DecimalFormat(format);
- return d.format(value);
- }
- /**
- * 描述:浮点格式化 value = 45.678 返回值 45.68
- * @param value
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String getFormatDouble(double value) {
- return getFormatDouble(value, "0.00");
- }
- /**
- * 描述:double数据类型取小数点位数
- * 例如:doubleData = 45.678,scale = 2 返回值 = 45.68
- * @param doubleData
- * @param scale
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String getDoubleByScale(double doubleData,int scale){
- String strFormator = "#." + org.apache.commons.lang.StringUtils.repeat("#",scale);
- java.text.DecimalFormat formater = new java.text.DecimalFormat(strFormator);
- String newData = formater.format(doubleData);
- return newData;
- }
- /**
- * 描述:判断str是否在arr String[]中
- * @param arr
- * @param str
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static boolean isContain(String[] arr,String str){
- if (null==arr) {
- return false;
- }
- for (int i = 0; i < arr.length; i++) {
- //equalsIgnoreCase 不区分大小写
- if (str.equalsIgnoreCase(arr[i])) {
- return true;
- }
- }
- return false;
- }
- /**
- * 描述:获取sub在string里的个数,例如 sub=123,string=123123,return=2
- * @param sub
- * @param string
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static int getSubCount(String sub, String string) {
- if (isBlank(string)) {
- return 0;
- }
- int subLength = sub.length();
- int forLength = string.length() - sub.length() + 1; // 循环次数
- int count = 0; // 记录循环次数
- for (int i = 0; i < forLength; i++) {
- if (org.apache.commons.lang.StringUtils.substring(string, i, i + subLength).equals(sub)) {
- count++;
- }
- }
- return count;
- }
- /**
- * 描述:数组转换为字符串
- * @param array
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String getArrayAsString(String[] array){
- String rs = "";
- if(array != null){
- for(int i =0;i < array.length;i ++){
- rs += array[i] + ";";
- }
- }
- //测试此字符串是否以指定的后缀结束
- if(rs.endsWith(";")){
- rs = rs.substring(0,rs.length() - 1);
- }
- return rs;
- }
- /**
- * 描述:输入带html标签的字符串,返回干净的字符串
- * @param body
- * @return
- * 作者:齐继超
- * 时间:Nov 29, 2010
- */
- public static String getCleanString(String body) {
- //替换 ->" ",<br>->/r/n <p>->/r/n/r/n
- body = body.replaceAll("&[nN][bB][sS][pP];"," ");
- body = body.replaceAll("<[bB][rR]//s*>","/r/n");
- body = body.replaceAll("<[pP]//s*>","/r/n/r/n");
- //删除所有标签
- body = body.replaceAll("<.+?>","");
- return body;
- }
- }
- 总结-处理字符串常用方法类
- JS常用字符串处理方法总结
- C#中几种常用的处理字符串的方法总结
- 常用方法2 ---字符串处理类
- C#总结---字符串类常用方法
- Shell处理字符串常用方法
- Shell处理字符串常用方法
- Shell处理字符串常用方法
- JavaScript常用字符串处理方法
- Shell处理字符串常用方法
- string字符串常用处理方法
- Shell处理字符串常用方法
- 常用的字符串处理方法
- Shell处理字符串常用方法
- Shell常用处理字符串方法
- 常用的字符串处理方法
- JavaScript常用字符串处理方法
- python字符串处理常用方法
- jquery 图片裁切预览,简单拖动
- vsftpd问题
- 简单实用的xml解析类-----------CMarkUP
- 两个DataTablel用Linq实现主外键查询
- setup-arm-elf-tools-20040427
- 总结-处理字符串常用方法类
- [摘抄]UBUNTU启动管理
- shell判断文件,目录是否存在或者具有权限
- 企业级任务调度框架Quartz 三 一个简单的Quartz 例子
- RMAN 系列(三) ---- 介质管理问题
- 自己总结出来的东西还是得谨慎
- SOA架构的十大技术理论体系
- Oracle中创建自增列
- java使用jdbc轻松连接访问access