Skip to content

Instantly share code, notes, and snippets.

@imjoey
Created June 10, 2015 06:23
Show Gist options
  • Save imjoey/8f152a7d894b1d21a7f1 to your computer and use it in GitHub Desktop.
Save imjoey/8f152a7d894b1d21a7f1 to your computer and use it in GitHub Desktop.
Java string utility functions, with some from the internet
package com.newcoresys.utils;
import java.util.Arrays;
import java.util.List;
/**
* @Description: 字符串处理工具类
* @author: joeymac
*/
public class StringEx {
/**
* 字符串首字母小写
*
* @param str
* @return
*/
public static String toLowerCaseFirstOne(String str) {
if (str == null || "".equals(str))
return str;
if (Character.isLowerCase(str.charAt(0)))
return str;
else
return (new StringBuilder())
.append(Character.toLowerCase(str.charAt(0)))
.append(str.substring(1)).toString();
}
/**
* 字符串首字母大写
*
* @param str
* @return
*/
public static String toUpperCaseFirstOne(String str) {
if (str == null || "".equals(str))
return str;
if (Character.isUpperCase(str.charAt(0)))
return str;
else
return (new StringBuilder())
.append(Character.toUpperCase(str.charAt(0)))
.append(str.substring(1)).toString();
}
/**
* 使用delimiter join字符串数组(默认capacity是32)
*
* @param delimiter
* @param values
* @return
* @author joeymac
*/
public static String join(String delimiter, String... values) {
return join(delimiter, Arrays.asList(values));
}
/**
* 使用delimiter join整型数组(默认capacity是32)
*
* @param delimiter
* @param values
* @returnj
* @author joeymac
*/
public static String join(String delimiter, Integer... values) {
return joinInteger(delimiter, Arrays.asList(values));
}
/**
* 使用delimiter join字符串数组(默认capacity是32)
*
* @param delimiter
* @param values
* @return
* @author joeymac
*/
public static String join(String delimiter, List<String> values) {
if (delimiter == null || values.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < values.size(); i++) {
if (i != 0) sb.append(delimiter);
if (values.get(i) != null) {
sb.append(values.get(i));
}
}
return sb.toString();
}
/**
* 使用delimiter join整型数组(默认capacity是32)
*
* @param delimiter
* @param values
* @return
* @author joeymac
*/
public static String joinInteger(String delimiter, List<Integer> values) {
if (delimiter == null || values.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < values.size(); i++) {
if (i != 0) sb.append(delimiter);
if (values.get(i) != null) {
sb.append(values.get(i));
}
}
return sb.toString();
}
/**
* 判断字符串是不是null
* @param str
* @return
*/
public static boolean isNull(String str) {
return str == null;
}
public static byte[] charToByte(char ch) {
int temp = (int) ch;
byte[] b = new byte[2];
// 将高8位放在b[0],将低8位放在b[1]
for (int i = 1; i > -1; i--) {
b[i] = (byte) (temp & 0xFF);// 低8位
// 向右移8位
temp >>= 8;
}
return b;
}
// 将字节转化为比特数组
public static byte[] byteToBitArray(byte b) {
// 强制转换成int?
int temp = (int) b;
byte[] result = new byte[8];
for (int i = 7; i > -1; i--) {
result[i] = (byte) (temp & 0x01);
temp >>= 1;
}
return result;
}
// 将二维比特数组转化为字节
public static byte bitToByteArray(byte[] b) {
byte result;
result = (byte) (b[7] | b[6] << 1 | b[5] << 2 | b[4] << 3 | b[3] << 4
| b[2] << 5 | b[1] << 6 | b[0] << 7);
return result;
}
// 将字节转化为字符
public static char byteToChar(byte[] b) {
int s = 0;
if (b[0] > 0) {
s += b[0];
}
if (b[0] < 0) {
s += 256 + b[0];
}
s *= 256;
if (b[1] > 0) {
s += b[1];
}
if (b[1] < 0) {
s += 256 + b[1];
}
char ch = (char) s;
return ch;
}
public static String byteToHexString(byte b) {
String hex = "";
hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex;
}
public static String bytesToHexString(byte[] bs) {
StringBuffer sb = new StringBuffer();
String hex = "";
for (int i = 0; i < bs.length; i++) {
hex = Integer.toHexString(bs[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex);
}
return sb.toString();
}
public static byte[] hexStringToBytes(String in) {
byte[] arrB;
try {
arrB = in.getBytes("UTF-8");
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println(hexStringToBytes("000a0a0a0a0202aa000a0a0a0a0202aa"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment