From e6af6e9841af48bde60504449e8f00b2bc9466a1 Mon Sep 17 00:00:00 2001 From: seagull <24163551@qq.com> Date: Mon, 25 Jun 2018 09:35:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E6=88=AA=E5=8F=96=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/luckyclient/driven/SubString.java | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/luckyclient/driven/SubString.java b/src/main/java/luckyclient/driven/SubString.java index 9b8256b..c4b673b 100644 --- a/src/main/java/luckyclient/driven/SubString.java +++ b/src/main/java/luckyclient/driven/SubString.java @@ -1,5 +1,8 @@ package luckyclient.driven; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class SubString { @@ -60,7 +63,7 @@ public class SubString { getstr=str.substring(start,end); } }else{ - getstr=""; + getstr="指定的开始或是结束位置字符不是数字类型,请检查!"; } return getstr; @@ -84,7 +87,7 @@ public class SubString { getstr=str.substring(start); } }else{ - getstr=""; + getstr="指定的开始位置字符不是数字类型,请检查!"; } return getstr; @@ -108,12 +111,40 @@ public class SubString { getstr=str.substring(0,end); } }else{ - getstr=""; + getstr="指定的结束位置字符不是数字类型,请检查!"; } return getstr; } + public static String subStrRgex(String str,String rgex,String num){ + List list = new ArrayList(); + Pattern pattern = Pattern.compile(rgex);// 匹配的模式 + Matcher m = pattern.matcher(str); + while (m.find()) { + int i = 1; + list.add(m.group(i)); + i++; + } + + String getstr=""; + if(isInteger(num)){ + int index=Integer.valueOf(num); + if(index<0){ + getstr="截取字符串索引数字不能小于0"; + }else if(index>str.length()){ + getstr="截取字符串索引的数字不能大于字符串本身的长度【"+str.length()+"】"; + }else if(index>list.size()){ + getstr="未能在指定字符串中根据正则式找到匹配的字符串或是指定的索引数字大于能找到的匹配字符串索引量"; + }else{ + getstr=list.get(index-1); + } + }else{ + getstr="指定的索引位置字符不是数字类型,请检查!"; + } + return getstr; + } + private static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches();