StringsUtil字符串工具类---灵活截取

package com.js.ai.modules.pointwall.interfac;
import javax.print.attribute.standard.MediaName;
public class StringsUtil {
/**
* @Title: join
* @Description: 用指定字符串数组相连接,并返回
* @param strs 字符串数组
* @param splitStr 连接数组的字符串
* @return
* @return: String
*/
public static String join(String[] strs, String splitStr){
if(strs!=null){
if(strs.length==1){
return strs[0];
}
StringBuffer sb = new StringBuffer();
for (String str : strs) {
sb.append(str).append(splitStr);
}
if(sb.length()>0){
sb.delete(sb.length()-splitStr.length(), sb.length());
}
return sb.toString();
}
return null;
}
/**
* @Title: subStrStart
* @Description: 从头开始截取
* @param str 字符串
* @param end 结束位置
* @return
* @return: String
*/
public static String subStrStart(String str, int end){
return subStr(str, 0, end);
} /**
*
* @Title: subStrEnd
* @Description: 从尾开始截取
* @param str 字符串
* @param start 开始位置
* @return
* @return: String
*/
public static String subStrEnd(String str, int start){
return subStr(str, str.length()-start, str.length());
} /**
* @Title: subStr
* @Description: 截取字符串 (支持正向、反向截取)
* @param str 待截取的字符串
* @param length 长度 ,>=0时,从头开始向后截取length长度的字符串;<0时,从尾开始向前截取length长度的字符串
* @return
* @throws RuntimeException
* @return: String
*/
public static String subStr(String str, int length) throws RuntimeException{
if(str==null){
throw new NullPointerException("字符串为null");
}
int len = str.length();
if(len<Math.abs(length)){
throw new StringIndexOutOfBoundsException("最大长度为"+len+",索引超出范围为:"+(len-Math.abs(length)));
}
if(length>=0){
return subStr(str, 0,length);
}else{
return subStr(str, len-Math.abs(length), len);
}
} /**
* 截取字符串 (支持正向、反向选择)
* @param str 待截取的字符串
* @param start 起始索引 ,>=0时,从start开始截取;<0时,从length-|start|开始截取
* @param end 结束索引 ,>=0时,从end结束截取;<0时,从length-|end|结束截取
* @return 返回截取的字符串
* @throws RuntimeException
*/
public static String subStr(String str, int start, int end) throws RuntimeException{
if(str==null){
throw new NullPointerException("");
}
int len = str.length();
int s = 0;//记录起始索引
int e = 0;//记录结尾索引
if(len<Math.abs(start)){
throw new StringIndexOutOfBoundsException("最大长度为"+len+",索引超出范围为:"+(len-Math.abs(start)));
}else if(start<0){
s = len - Math.abs(start);
}else if(start<0){
s=0;
}else{//>=0
s = start;
}
if(len<Math.abs(end)){
throw new StringIndexOutOfBoundsException("最大长度为"+len+",索引超出范围为:"+(len-Math.abs(end)));
}else if (end <0){
e = len - Math.abs(end);
}else if (end==0){
e = len;
}else{//>=0
e = end;
}
if(e<s){
throw new StringIndexOutOfBoundsException("截至索引小于起始索引:"+(e-s));
} return str.substring(s, e);
}
public static void main(String[] args) {
String str = "abcde12345678";
System.out.println("--------------------------------");
System.out.println("正向截取长度为3,结果:\n" + StringsUtil.subStr(str, 3));
System.out.println("反向截取长度为4,结果:\n" + StringsUtil.subStr(str, -4));
System.out.println("--------------------------------");
System.out.println("正向截取到第4个字符的位置,结果:\n" + StringsUtil.subStrStart(str, 4));
System.out.println("反向截取到第4个字符的位置,结果:\n" + StringsUtil.subStrEnd(str, 4));
System.out.println("--------------------------------");
System.out.println("从第2个截取到第9个,结果:\n" + StringsUtil.subStr(str, 1, 9));
System.out.println("从第2个截取到倒数第1个,结果:\n" + StringsUtil.subStr(str, 1, -1));
System.out.println("从倒数第4个开始截取,结果:\n" + StringsUtil.subStr(str, -4, 0));
System.out.println("从倒数第4个开始截取,结果:\n" + StringsUtil.subStr(str, -4, 10));
}
}

  

上一篇:Android Layout_weight 属性


下一篇:数据库MySQL基本语法思维导图