Commit 739eb1f2930e43842fdc8b0c50c5a7f8dbbc3a84
1 parent
90f6a1cf
Exists in
master
1.add regex func
Showing
1 changed file
with
47 additions
and
0 deletions
Show diff stats
src/main/java/com/taover/util/UtilString.java
| ... | ... | @@ -8,6 +8,53 @@ import java.util.regex.Matcher; |
| 8 | 8 | import java.util.regex.Pattern; |
| 9 | 9 | |
| 10 | 10 | public class UtilString { |
| 11 | + public static String trimByRegexS(String source){ | |
| 12 | + return trimLeftByRegexS(trimRightByRegexS(source)); | |
| 13 | + } | |
| 14 | + | |
| 15 | + public static String trimLeftByRegexS(String source){ | |
| 16 | + if(source == null){ | |
| 17 | + return null; | |
| 18 | + } | |
| 19 | + | |
| 20 | + Pattern pattern = Pattern.compile("\\S"); | |
| 21 | + int startIndex = -1; | |
| 22 | + for(int i=0; i<source.length(); ++i){ | |
| 23 | + char data = source.charAt(i); | |
| 24 | + if(pattern.matcher(data+"").find()){ | |
| 25 | + startIndex = i; | |
| 26 | + break; | |
| 27 | + } | |
| 28 | + } | |
| 29 | + if(startIndex > -1){ | |
| 30 | + return source.substring(startIndex); | |
| 31 | + }else{ | |
| 32 | + return ""; | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static String trimRightByRegexS(String source){ | |
| 37 | + if(source == null){ | |
| 38 | + return null; | |
| 39 | + } | |
| 40 | + | |
| 41 | + Pattern pattern = Pattern.compile("\\S"); | |
| 42 | + int endIndex = source.length(); | |
| 43 | + for(int i=source.length()-1; i>=0; --i){ | |
| 44 | + char data = source.charAt(i); | |
| 45 | + if(pattern.matcher(data+"").find()){ | |
| 46 | + endIndex = i; | |
| 47 | + break; | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + if(endIndex > -1){ | |
| 52 | + return source.substring(0, endIndex+1); | |
| 53 | + }else{ | |
| 54 | + return ""; | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 11 | 58 | public static String trimByRegexW(String pattern){ |
| 12 | 59 | Matcher m = Pattern.compile("\\w+").matcher(pattern); |
| 13 | 60 | String result = ""; | ... | ... |