工作中碰到的Java问题整理及解决方案

版权声明:个人 https://blog.csdn.net/csdnmrliu/article/details/82876006

1.SOH分隔符(Notepad++)

SOH是ASCII码表为ASCII值==1的控制字符(不可见)char字符:0x01

完整ASCII码表见:https://www.cnblogs.com/myblesh/articles/2495378.html

Java String.split() 以SOH为分隔符的字符串

//第一种方案(最优解决方案)
lineTxt.split("\001", -1);

//第二种方案和第一种类似
private static final byte[] SOH = { 0x01 };
private static final String SPLIT = new String(SOH);
lineTxt.split(SPLIT, -1);

//第三种解决方案比较麻烦(之前不知道第一种解决方案)
//先把SOH字符替换为‘|’,再使用split("\\|", -1);
public static String conver(String lineTxt){
	char[] cs = lineTxt.toCharArray();
	StringBuilder builder = new StringBuilder();
	for (char c : cs) {
		if(c == 0x01){
			builder.append("|");
		}else{
			builder.append(c);
		}
	}
	return builder.toString();
}

测试第一种方案

/**
 * 测试SOH控制字符为分隔符
 */
@Test
public void test(){
	char c = 0x01;
	String xdrstr = "2018072607182018-07-26 07:18:34.3422018-07-26 07:18:34.3744600773516111053532680788981525353268076\\N25525525525565535\\N65535241763786765535255429496729532416838592749884294967295255.255.255.255255.255.255.255\\N100.84.4.1\\N100.84.88.249364123641265535429496729513648107873458IMS.mnc000.mcc460.gprs1861573551700318051912552557098226391199377408\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N\\N03555\\N5b59054a0000bd7600bd7601b249388c";
	System.out.println(xdrstr.split("\001")[1]);
}

猜你喜欢

转载自blog.csdn.net/csdnmrliu/article/details/82876006
今日推荐