Skip to content

Instantly share code, notes, and snippets.

@futeng
Created May 22, 2013 16:44
Show Gist options
  • Save futeng/5629046 to your computer and use it in GitHub Desktop.
Save futeng/5629046 to your computer and use it in GitHub Desktop.
字符串解析工具集。 v1.0 新增slice方法,运行对一个字符串通过指定收尾字串来切割并获取中间串,类似与对字符串切两刀,保留中间的字符。
/**
* Copyright (c) 2013 by futeng www.futeng.org
*
* @{#} @StringParseUtils.java Create on @2013-5-22 @下午6:03:03
*/
package com.ustcinfo.inm.data.bolt.parser.utils;
/**
*
* @author <a href="mailto:ifuteng@gmail.com">futeng</a>
*
*/
public class StringParseUtils {
public static String slice(String from, String to, String target, String prefixNeedRemove) {
String str = slice(from, to, target, true);
if (str.startsWith(prefixNeedRemove)) {
return str.substring(prefixNeedRemove.length()).trim();
}
return str;
}
public static String slice(String from, String to, String target, boolean needTrim) {
if(!hasText(from ,to, target) || !contains(target, from, to)) {
try {
throw new Exception("StringParseUtils#slice method need three parameters, please macke sure everyone has content(whitespace or empty are all illegal).");
} catch (Exception e) {
e.printStackTrace();
}
}
String str = target;
int beginIndex = str.indexOf(from)+from.length();
if(beginIndex == str.length()) {
try {
throw new Exception("Can not slice the String, there is no empty left.");
} catch (Exception e) {
e.printStackTrace();
}
}
int endIndex = str.indexOf(to);
if(endIndex < beginIndex) {
try {
throw new Exception("Can not slice the String, make sure the start slice piece before the end slice piece.");
} catch (Exception e) {
e.printStackTrace();
}
}
return needTrim ? str.substring(beginIndex, endIndex).trim() : str.substring(beginIndex, endIndex);
}
public static String slice(String from, String to, String target) {
return slice(from, to, target, true);
}
/**
* Check whether the given CharSequence's length >0
*/
public static boolean hasLength(CharSequence str) {
return (str != null && str.length() > 0);
}
/**
* Check whether the given CharSequence has actual text(whitespace will return false).
*/
public static boolean hasText(CharSequence str) {
if (!hasLength(str)) {
return false;
}
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
/**
* Check whether the given String has actual text(Whitespace will return false).
*/
public static boolean hasText(String str) {
return hasText((CharSequence) str);
}
/**
* Check whether the given String array has actual text(Whitespace will return false).
* <p><pre>
* AlarmStringUtils.hasText(" a","pretty","girl ","!") = true
* AlarmStringUtils.hasText(" ","so ","beautiful") = false;
* AlarmStringUtils.hasText("","do","you like?") = false;
* </pre>
* @param strs the String array to check (may be <code>null</code>)
* @return <code>true</code> if the String is not <code>null</code>, its length is
* greater than 0, and it does not contain whitespace only
* @see #hasText(String)
*/
public static boolean hasText(String ...strs) {
int len = strs.length;
while(len>0) {
if (! hasText(strs[strs.length-(len--)]) ) {
return false;
}
}
return true;
}
/**
* Check whether the target String contains all of element from the String array.
*/
public static boolean contains(String target, String... sources) {
if (!hasText(target) || !hasText(sources) ) {
return false;
}
for (String str : sources) {
if (!target.contains(str)) {
return false;
}
}
return true;
}
/*public static void main(String[] args) {
String target = "<+++>"+"\r\n"
+ "设备告警流水号 = 21173"+"\r\n"
+ "网络流水号 = 15182840"+"\r\n"
+ "对象标识 = .3221229568.3221233664.3221282824.3223113732.3222324262"+"\r\n"
+ "对象名称 = DC城阳小寨子m1(工)"+"\r\n"
+ "对象类型 = BTS"+"\r\n"
+ "网元标识 = .3221229568.3221233664.3221282824"+"\r\n"
+ "网元名称 = 青岛BSC23"+"\r\n"
+ "网元类型 = BSC6000(R11+)"+"\r\n"
+ "告警ID = 26232"+"\r\n"
+ "告警名称 = BBU光模块收发异常告警"+"\r\n"
+ "告警种类 = 故障"+"\r\n"
+ "告警级别 = 重要"+"\r\n"
+ "告警状态 = 未确认 & 未恢复"+"\r\n"
+ "告警类型 = 硬件系统"+"\r\n"
+ "发生时间 = 2013-03-17 00:00:40"+"\r\n"
+ "定位信息 = 柜号=0, 框号=0, 槽号=6, 端口号=0, 单板类型=GTMU, 具体问题=2, 站点号=163, 站点类型=DBS3900 GSM, 站点名称=DC城阳小寨子m1(工)"+"\r\n"
+ "<--->";
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
slice("定位信息 ","<--->",target,"=");
}
System.out.println("Spend Time:"+(System.currentTimeMillis()-startTime)+"ms");
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment