⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gfstring.java

📁 基于java语言的分词系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

	/**
	 * 在指定字符串插入到源字符串的指定位置
	 * 
	 * @param src
	 *            源字符串
	 * @param insertStr
	 *            要插入的字符串
	 * @param index
	 *            要插入的位置
	 * @return 插入指定的字符之后的字符串
	 */
	public static String insert(String src, String insertStr, int index) {
		String result = src;

		if (src != null && insertStr != null) {
			String temp = null;

			if (index < 0) {
				if (index * -1 > src.length())
					result = insertStr + src;
				else {
					temp = src.substring(src.length() + index + 1);
					result = src.substring(0, src.length() + index + 1) + insertStr + temp;
				}
			} else if (index >= src.length())
				result = src + insertStr;
			else {
				temp = src.substring(index);
				result = src.substring(0, index) + insertStr + temp;
			}
		} else if (src == null && insertStr != null)
			result = insertStr;
		return result;

	}

	/**
	 * 把相临的两个字符对换,字符串长度为奇数时最后加“F”
	 * 
	 * @param src
	 * @return
	 */
	public static String interChange(String src) {
		String result = null;

		if (src != null) {
			if (src.length() % 2 != 0)
				src += "F";
			src += "0";
			result = "";
			for (int i = 0; i < src.length() - 2; i += 2) {
				result += src.substring(i + 1, i + 2);
				result += src.substring(i, i + 1);
			}
		}

		return result;
	}

	/**
	 * 把数组按指定的编码方式转化成字符串
	 * 
	 * @param b
	 *            源数组
	 * @param encoding
	 *            编码方式
	 * @return
	 */
	public static String bytes2str(byte[] b, String encoding) {
		String result = null;
		int actualLen = 0;
		byte[] ab;

		if (b != null && b.length > 0) {
			for (int i = 0; i < b.length; i++) {
				if (b[i] == 0)
					break;
				actualLen++;
			}
			ab = GFCommon.bytesCopy(b, 0, actualLen);
			try {
				result = new String(ab, encoding);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

		}
		return result;
	}

	/**
	 * 把一个字符串按指定的长度分割
	 * 
	 * @param intervalLen
	 *            间隔长度
	 * @return
	 */
	public static String[] split(String src, int intervalLen) {
		String[] result = null;
		int len = 0;

		if (src != null && intervalLen > 0) {
			len = src.length() / intervalLen;

			if (src.length() % intervalLen != 0)
				len++;

			result = new String[len];

			for (int i = 0, j = 0; i < len - 1; i++, j += intervalLen)
				result[i] = src.substring(j, j + intervalLen);
			result[len - 1] = src.substring((len - 1) * intervalLen);
		}

		return result;
	}

	/**
	 * 把字节数组转化成十六进制的字符串
	 * 
	 * @param b
	 * @return
	 */
	public static String bytes2hexstr(byte[] b) {
		return bytes2hexstr(b, false);
	}

	/**
	 * 把字节数组转化成十六进制的字符串
	 * <p>
	 * 
	 * @param b
	 * @param highBitFirst
	 *            true:高位优先,即输出的十六进制字符串是从Byte数组的最大下标开始的
	 *            false:低们优先,即输出的十六进制字符串是从Byte数组的最小下标0开始的
	 * @return
	 */
	public static String bytes2hexstr(byte[] b, boolean highBitFirst) {
		String result = null;

		if (b != null && b.length > 0) {
			if (highBitFirst) {
				for (int i = b.length - 1; i >= 0; i--) {
					String hex = GFString.byte2hex(b[i]);
					if (result == null)
						result = hex;
					else
						result += hex;
				}
				result = result.toUpperCase();
			} else {
				for (int i = 0; i < b.length; i++) {
					String hex = GFString.byte2hex(b[i]);
					if (result == null)
						result = hex;
					else
						result += hex;
				}
				result = result.toUpperCase();
			}
		}
		return result;
	}

	/**
	 * 把字节数组转化成十六进制的字符串
	 * 
	 * @param b
	 * @return
	 */
	public static String bytes2hexstr(byte[] b, int len) {
		String result = null;

		if (b != null && b.length > 0 && len <= b.length) {
			for (int i = 0; i < len; i++) {
				String hex = GFString.byte2hex(b[i]);
				if (result == null)
					result = hex;
				else
					result += hex;
			}

			result = result.toUpperCase();
		}
		return result;
	}

	/**
	 * 把十六进制字符串转化成字节数组 如果长度不是偶数的话,前面加“0”
	 * 
	 * @param hexstr
	 * @return
	 */
	public static byte[] hexstr2bytes(String hexstr) {
		byte[] b = null;
		int len = 0;

		if (hexstr != null) {

			if (hexstr.length() % 2 != 0)
				hexstr = "0" + hexstr;
			len = hexstr.length() / 2;
			b = new byte[len];

			String temp = hexstr + "0";
			for (int i = 0, j = 0; i < temp.length() - 2; i += 2, j++) {
				b[j] = GFString.hex2byte(temp.substring(i, i + 2));

			}
		}
		return b;
	}

	public static int hexstr2int(String hex) {
		if (hex != null && hex.length() <= 8) {
			hex = hex.toUpperCase();
			for (int i = 0; i < hex.length(); i++) {
				int value = hex.charAt(i);
				if (value < 48 || (value > 57 && value < 65) || value > 70)
					return 0;
			}

			byte[] b = hexstr2bytes(hex);
			return GFCommon.bytes2int(b);

		}
		return 0;
	}

	public static String getChineseString(byte[] bArray, String charsetName) {
		String ch = null;
		if (charsetName != null) {

			try {
				ch = new String(bArray, charsetName);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		return ch;

	}

	public static String bytes2str(byte[] b) {
		String result = null;
		int actualLen = 0;

		if (b != null && b.length > 0) {
			for (int i = 0; i < b.length; i++) {
				if (b[i] == 0)
					break;
				actualLen++;
			}
			byte[] b2 = GFCommon.bytesCopy(b, 0, actualLen);
			if (b2 != null && b2.length > 0)
				result = new String(b2);
		}
		return result;
	}

	/**
	 * 把整数转换成指定长度的字符串 如果指定长度小于整数的字符串长度,则只取前面LEN个字符。 如果LEN小0,则返回整数的字符串表示型式
	 * 
	 * @param value
	 * @param len
	 * @return
	 */
	public static String int2str(int value, int len) {
		String result = "" + value;
		int l = result.length();

		if (len >= 0) {
			if (l <= len) {
				for (int i = 0; i < len - l; i++)
					result = "0" + result;
			} else {
				result = result.substring(0, len);
			}
		}

		return result;
	}

	/**
	 * 把十六进制数转化成字节
	 * 
	 * @param hex
	 * @return
	 */
	public static byte hex2byte(String hex) {
		byte b = 0;
		int value = 0;

		if (hex != null && hex.length() <= 2) {
			hex = hex.toUpperCase();
			if (hex.length() == 0)
				return 0;
			else if (hex.length() >= 1) {
				value = hex.charAt(0);
				if (value < 48 || (value > 57 && value < 65) || value > 70)
					return 0;

				if (hex.length() == 2) {
					value = hex.charAt(1);
					if (value < 48 || (value > 57 && value < 65) || value > 70)
						return 0;
				}
			}

			try {
				b = (byte) Integer.parseInt(hex, 16);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return b;
	}

	/**
	 * 把字节转换成十六进制字符串,固定为两个字符长度
	 * 
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte b) {
		String result = null;

		result = Integer.toHexString((b >> 4) & 0x0f);
		result += Integer.toHexString(b & 0xf);
		return result.toUpperCase();
	}

	/**
	 * 用UTF-16BE的编码方式把含有全角编码的字符串转成半角编码的字符串
	 * <p>
	 * 比如把“汉##012012YY”转成“汉##012012YY”
	 * <p>
	 * 对全角的空格处理还有问题
	 * 
	 * @param str
	 * @return
	 */
	public static String quan2ban(String str) {
		String result = null;

		if (str != null) {
			try {
				byte[] uniBytes = str.getBytes("utf-16be");
				byte[] b = new byte[uniBytes.length];
				for (int i = 0; i < b.length; i++) {
					if (uniBytes[i] == -1) {
						b[i] = 0;
						if (i + 1 < uniBytes.length)
							b[++i] = (byte) (uniBytes[i] + 0x20);

					} else
						b[i] = uniBytes[i];
				}

				result = new String(b, "utf-16be");
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 用UTF-16BE的编码方式把含有半角的字符串转成全角字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String ban2quan(String str) {
		String result = null;

		if (str != null) {
			try {
				byte[] uniBytes = str.getBytes("utf-16be");
				byte[] b = new byte[uniBytes.length];
				for (int i = 0; i < b.length; i++) {
					if (uniBytes[i] == 0) {
						b[i] = -1;
						if (i + 1 < uniBytes.length)
							b[++i] = (byte) (uniBytes[i] - 0x20);

					} else
						b[i] = uniBytes[i];
				}
				result = new String(b, "utf-16be");
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 用GBK编码进行全角转半角
	 * 
	 * @param str
	 * @return
	 */
	public static String quan2banGBK(String str) {
		String result = null;

		if (str != null) {
			try {
				int j = 0;
				byte[] uniBytes = str.getBytes("GBK");
				byte[] b = new byte[uniBytes.length];
				for (int i = 0; i < b.length; i++) {
					if (uniBytes[i] == (byte) 0xA3) {
						if (i + 1 < uniBytes.length)
							b[j] = (byte) (uniBytes[++i] - 0x80);
					} else {
						b[j] = uniBytes[i];
						if (uniBytes[i] < 0 && i + 1 < b.length)
							b[++j] = uniBytes[++i];

					}
					j++;
				}
				result = new String(b, 0, j, "GBK");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	// CDA3 D6B9 20 BABA 23 A3A3 303132 A3B0A3B1A3B259A3D939D3AC39A3D9

	/**
	 * 用GBK编码进行半角转全角
	 * <p>
	 * 从每个字节判起,如果一个字节的值不大于0X7F,则它是Ascii码的字符。进入下一个判断。
	 * <p>
	 * 如果一个字节的值大于0X81,且紧跟着它的下一个字节的值在0x40--0xFE之间,则是汉字或全角字符
	 * 
	 * @param str
	 * @return
	 */
	public static String ban2quanGBK(String str) {
		String result = null;

		if (str != null) {
			try {
				int j = 0;
				byte[] uniBytes = str.getBytes("GBK");
				byte[] b = new byte[uniBytes.length * 2];
				for (int i = 0; i < uniBytes.length; i++) {
					if (uniBytes[i] >= 0) {
						b[j] = (byte) 0xA3;
						if (j + 1 < b.length)
							b[++j] = (byte) (uniBytes[i] + 0x80);

					} else {
						b[j] = uniBytes[i];
						if (i + 1 < uniBytes.length && j + 1 < b.length)
							b[++j] = uniBytes[++i];
					}

					j++;
				}
				result = new String(b, 0, j, "GBK");
			} catch (UnsupportedEncodingException e) {

				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 去掉字符串中的空白符
	 * 
	 * @param s
	 * @return
	 */
	public static String removeSpace(String s) {
		String rs = null;
		String s1 = null;

		if (s != null) {
			s += " ";
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < s.length(); i++) {
				s1 = s.substring(i, i + 1);
				if (!s1.equals(" "))
					sb.append(s1);
			}

			rs = sb.toString();
		}
		return rs;
	}

	/**
	 * 对字符串中的空格进行格式化,去掉开头和最后的空格,把字符之间的空格缩减为1个.
	 * <p>
	 * 比如:<空格><空格>我是一个人<空格><空格><空格>中国人<空格>大学生<空格><空格>
	 * <p>
	 * 结果应该为:我是一个人<空格>中国人<空格>大学生
	 * 
	 * @param src
	 * @return
	 */
	public static String formatSpace(String src) {
		String result = null;

		if (src != null) {
			result = "";
			String[] ss = src.split(" ");
			for (int i = 0; i < ss.length; i++) {
				if (ss[i] != null && ss[i].length() > 0) {
					result += ss[i] + " ";
				}
			}

			if (result.length() > 0 && result.substring(result.length() - 1).equals(" "))
				result = result.substring(0, result.length() - 1);
		}

		return result;
	}

	/**
	 * 7-BIT编码 把ASCII码值最高位为0的字符串进行压缩转换成8位二进制表示的字符串
	 * 
	 * @param src
	 * @return
	 */
	public static String encode7bit(String src) {
		String result = null;
		String hex = null;
		byte value;

		if (src != null && src.length() == src.getBytes().length) {
			result = "";
			byte left = 0;
			byte[] b = src.getBytes();
			for (int i = 0, j = 0; i < b.length; i++) {
				j = i & 7;
				if (j == 0)
					left = b[i];
				else {
					value = (byte) ((b[i] << (8 - j)) | left);
					left = (byte) (b[i] >> j);
					hex = GFString.byte2hex((byte) value);
					result += hex;
					if (i == b.length - 1)
						result += GFString.byte2hex(left);
				}
			}

			result = result.toUpperCase();
		}
		return result;
	}

	/**
	 * 对7-BIT编码进行解码
	 * 
	 * @param src
	 *            十六进制的字符串,且为偶数个
	 * @return 源字符串
	 */
	public static String decode7bit(String src) {
		String result = null;
		int[] b;
		String temp = null;
		byte srcAscii;
		byte left = 0;

		if (src != null && src.length() % 2 == 0) {
			result = "";
			b = new int[src.length() / 2];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -