Java 字符串工具类

2023-12-16 20:06:54

功能

集成 查询、分割、替换、首字母大写(小写),部分截取、计数、判断是否合法,是否整数,网络上用到的escape和unescape、unicode解码等等功能

代码

package utils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.log.Logx;

public class StringUtils
{
   private static char C0 = "0".charAt(0);
   private static char C9 = "9".charAt(0);

   /**
   * 首字母大写
   */
   public static String ucfirst(String str)
   {
   	if (str == null || str.isEmpty())
   		return str;

   	return (str.charAt(0) + "").toUpperCase() + str.substring(1);
   }

   /**
   * 首字母小写
   */
   public static String lcfirst(String str)
   {
   	if (str == null || str.isEmpty())
   		return str;

   	return (str.charAt(0) + "").toLowerCase() + str.substring(1);
   }
   
//	public static String fromBuffer( IoBuffer buf )
//	{
//		return new String(buf.array());
//	}

   /**
    * 获取字符串的长度,中文占一个字符,英文数字占半个字符
    *
    * @param value 指定的字符串
    * @return 字符串的长度
    */
   public static int lengthZW(String text)
   {
   	String Reg = "^[\u4e00-\u9fa5]{1}$";// 正则
   	int result = 0;
   	for (int i = 0; i < text.length(); i++)
   	{
   		String b = Character.toString(text.charAt(i));
   		if (b.matches(Reg))
   			result++;
   	}
   	return result;
   }
   
   public static String escape(String src)
   {
   	int i;
   	char j;
   	StringBuffer tmp = new StringBuffer();
   	tmp.ensureCapacity(src.length() * 6);

   	for (i = 0; i < src.length(); i++)
   	{
   		j = src.charAt(i);

   		if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
   			tmp.append(j);
   		else if (j < 256)
   		{
   			tmp.append("%");
   			if (j < 16)
   				tmp.append("0");
   			tmp.append(Integer.toString(j, 16));
   		} 
   		else
   		{
   			tmp.append("%u");
   			tmp.append(Integer.toString(j, 16));
   		}
   	}
   	return tmp.toString();
   }

   public static String unescape(String src)
   {
   	StringBuffer tmp = new StringBuffer();
   	tmp.ensureCapacity(src.length());
   	int lastPos = 0, pos = 0;
   	char ch;
   	while (lastPos < src.length())
   	{
   		pos = src.indexOf("%", lastPos);
   		if (pos == lastPos)
   		{
   			if (src.charAt(pos + 1) == 'u')
   			{
   				ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
   				tmp.append(ch);
   				lastPos = pos + 6;
   			} 
   			else
   			{
   				ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
   				tmp.append(ch);
   				lastPos = pos + 3;
   			}
   		}
   		else
   		{
   			if (pos == -1)
   			{
   				tmp.append(src.substring(lastPos));
   				lastPos = src.length();
   			} 
   			else
   			{
   				tmp.append(src.substring(lastPos, pos));

   				lastPos = pos;
   			}
   		}
   	}
   	return tmp.toString();
   }

   /**
    * 获取utf8文件的前面有几个无用字符
    * 
    * @param bytes
    * @return
    */
   public static int getUtfPrevIndex(byte[] bytes)
   {
   	if ((bytes[0] == (byte) 0x00) && (bytes[1] == (byte) 0x00) && (bytes[2] == (byte) 0xFE)
   			&& (bytes[3] == (byte) 0xFF))
   	{
   		// "UTF-32BE";
   		return 4;
   	} else if ((bytes[0] == (byte) 0xFF) && (bytes[1] == (byte) 0xFE) && (bytes[2] == (byte) 0x00)
   			&& (bytes[3] == (byte) 0x00))
   	{
   		// "UTF-32LE";
   		return 4;
   	} else if ((bytes[0] == (byte) 0xEF) && (bytes[1] == (byte) 0xBB) && (bytes[2] == (byte) 0xBF))
   	{
   		// "UTF-8";
   		return 3;
   	} else if ((bytes[0] == (byte) 0xFE) && (bytes[1] == (byte) 0xFF))
   	{
   		// "UTF-16BE";
   		return 2;
   	} else if ((bytes[0] == (byte) 0xFF) && (bytes[1] == (byte) 0xFE))
   	{
   		// "UTF-16LE";
   		return 2;
   	}

   	return 0;
   }

   public static String getUtf8FileString(byte[] bytes)
   {
   	int pl = getUtfPrevIndex(bytes);
   	return new String(bytes, pl, bytes.length - pl);
   }

   public static String parseWww(String www)
   {
   	if (www == null)
   		return "";

   	www = www.toLowerCase();
   	www = www.trim();

   	int len = www.length();
   	int i = len - 1;
   	for (; i >= 0; i--)
   	{
   		char c = www.charAt(i);
   		if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))
   		{
   		} else
   			break;
   	}
   	www = www.substring(i + 1);

   	return www;
   }

   public static String getTwo(String s)
   {
   	if (s.length() > 1)
   		return s;
   	return "0" + s;
   }

   public static String getTwo(int s)
   {
   	return getTwo(Integer.toString(s));
   }

   public static String getTwo(byte s)
   {
   	int v = s & 0xff;
   	return getTwo(Integer.toString(v));
   }

   public static String getTwo16(byte s)
   {
   	int v = s & 0xff;
   	return getTwo(Integer.toHexString(v));
   }

   public static boolean isValidString(String s)
   {
   	for (int i = 0; i < s.length(); i++)
   	{
   		char c = s.charAt(i);

   		// 可以用replaceAll(""+(char)65279,"")处理先
   		if ((c & 0xffff) == 65279)
   			continue;// 种编码方式一般会在windows操作系统中出现,比如WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF
   						// 0xBB 0xBF,即BOM)
   		if (c < '\t' || c > '~')
   			return false;
   	}
   	return true;
   }

   public static boolean isLegal(String value)
   {
   	if (value == null)
   		return false;
   	if (value.isEmpty())
   		return false;

   	int len = value.length();
   	char c;
   	for (int i = 0; i < len; i++)
   	{
   		c = value.charAt(i);

   		if (c == '_' || c == '-' || (c >= C0 && c <= C9) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
   		{
   			continue;
   		} else
   			return false;
   	}
   	return true;
   }

   /**
    * 纯数字或英文
    * 
    * @param value
    * @return
    */
   public static boolean isLegal2(String value)
   {
   	if (value == null)
   		return false;
   	if (value.isEmpty())
   		return false;

   	int len = value.length();
   	char c;
   	for (int i = 0; i < len; i++)
   	{
   		c = value.charAt(i);

   		if (c == '_' || (c >= C0 && c <= C9) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
   		{
   			continue;
   		} else
   			return false;
   	}
   	return true;
   }
   public static boolean isLegal2(char c)
   {
   	if (c == '_' || (c >= C0 && c <= C9) || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
   	{
   		return true;
   	}
   	return false;
   }

   public static boolean isIntString(String value)
   {
   	int len = value.length();
   	int c;
   	for (int i = 0; i < len; i++)
   	{
   		c = value.charAt(i);
   		if (c < C0 || c > C9)
   		{
   			return false;
   		}
   	}
   	return true;
   }

   public static String toHexString(byte b)
   {
   	return Integer.toHexString(b & 0xff);
   }

   public static String toHexString2(byte b)
   {
   	return getTwo(toHexString(b));
   }
   
   public static void trace( byte[] bytes, String sp )
   {
   	try
   	{
   		String s = "";
   		int len = bytes.length;
   		for( int i=0; i<len; i++ )
   		{
   			if( i > 0 ) s += ",";
   			s += Integer.toString( bytes[i]&0xff, 16 );
   		}
   		
   		Logx.log(sp + s);
   	}
   	catch( Exception er )
   	{
   		Logx.log("trace buff error " + er.getMessage());
   	}
   }

   public static int count(String str, String strCount)
   {
   	int t = 0;
   	int i = 0;
   	while ((i = str.indexOf(strCount, i)) != -1)
   	{
   		i++;
   		t++;
   	}
   	return t;
   }

   public static String[] split(String str, String reg)
   {
   	ArrayList<String> arr = new ArrayList<String>();

   	int t = 0;
   	int i = 0;
   	int len = reg.length();
   	while ((i = str.indexOf(reg, i)) != -1)
   	{
   		arr.add(str.substring(t, i));

   		i += len;
   		t = i;
   	}
   	arr.add(str.substring(t));

   	String[] ret = new String[arr.size()];
   	return arr.toArray(ret);
   }

   /**
   * 一个或多个空格分割
   */
   public static String[] splitBySpace(String s)
   {
   	return s.split("\\s+");
   }

   public static String replace(String s, int selFrom, int selTo, String replace)
   {
   	return s.substring(0, selFrom) + replace + s.substring(selTo);
   }

   public static String getStr(String data, int index, String begin, String end)
   {
   	int b = data.indexOf(begin, index);
   	if (b >= 0)
   	{
   		int e = data.indexOf(end, b + begin.length());
   		if (e >= 0)
   		{
   			return data.substring(begin.length() + b, e);
   		}
   	}

   	return null;
   }

   public static String getStr(StringBuffer data, int index, String begin, String end)
   {
   	int b = data.indexOf(begin, index);
   	if (b >= 0)
   	{
   		int e = data.indexOf(end, b + begin.length());
   		if (e >= 0)
   		{
   			return data.substring(begin.length() + b, e);
   		}
   	}

   	return null;
   }

   /**
    * @Description: unicode解码
    * @param str
    * @return
    */
   public static String unicodeDecode(String string)
   {
   	Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
   	Matcher matcher = pattern.matcher(string);
   	char ch;
   	while (matcher.find())
   	{
   		ch = (char) Integer.parseInt(matcher.group(2), 16);
   		string = string.replace(matcher.group(1), ch + "");
   	}
   	return string;
   }

   public static String removeLeft(String s, char rm)
   {
   	if (s == null)
   		return "";
   	int len = s.length();
   	for (int i = 0; i < len; i++)
   	{
   		if (s.charAt(i) != rm)
   			return s.substring(i);
   	}

   	return "";
   }
}

文章来源:https://blog.csdn.net/kael_wyh/article/details/135019196
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。