Commit 5eaf1409b991bef8a053babbee4789f6d25c2626
1 parent
156ea4df
Exists in
master
1 add func to trim by regex
Showing
2 changed files
with
45 additions
and
4 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilString.java
... | ... | @@ -4,8 +4,42 @@ import java.text.SimpleDateFormat; |
4 | 4 | import java.util.ArrayList; |
5 | 5 | import java.util.Date; |
6 | 6 | import java.util.List; |
7 | +import java.util.regex.Matcher; | |
8 | +import java.util.regex.Pattern; | |
7 | 9 | |
8 | 10 | public class UtilString { |
11 | + public static String trimByRegexW(String pattern){ | |
12 | + Matcher m = Pattern.compile("\\w+").matcher(pattern); | |
13 | + String result = ""; | |
14 | + while(m.find()){ | |
15 | + String currGroup = m.group(); | |
16 | + result += currGroup; | |
17 | + } | |
18 | + return result; | |
19 | + } | |
20 | + | |
21 | + public static String trimLeftByRegexW(String pattern){ | |
22 | + String tempPattern = new String(pattern); | |
23 | + Pattern p = Pattern.compile("\\w+"); | |
24 | + for(int i=0; i<tempPattern.length(); ++i){ | |
25 | + if(p.matcher(tempPattern.charAt(i)+"").matches()){ | |
26 | + return tempPattern.substring(i); | |
27 | + } | |
28 | + } | |
29 | + return ""; | |
30 | + } | |
31 | + | |
32 | + public static String trimRightByRegexW(String pattern){ | |
33 | + String tempPattern = new String(pattern); | |
34 | + Pattern p = Pattern.compile("\\w+"); | |
35 | + for(int i=tempPattern.length()-1; i>=0; --i){ | |
36 | + if(p.matcher(tempPattern.charAt(i)+"").matches()){ | |
37 | + return tempPattern.substring(0, i+1); | |
38 | + } | |
39 | + } | |
40 | + return ""; | |
41 | + } | |
42 | + | |
9 | 43 | public static String trimCodePage(String data){ |
10 | 44 | return data.replaceAll(new String(new byte[]{-30, -128, -83}), ""); |
11 | 45 | } |
... | ... | @@ -175,10 +209,17 @@ public class UtilString { |
175 | 209 | // System.out.println(getBeanNameFormTableName("asdf_asdf")); |
176 | 210 | // System.out.println(System.currentTimeMillis()); |
177 | 211 | // System.out.println(getCodeWithPreffix(1231212, 10, '-')); |
178 | - String dd = "\"{\"success\":true,\"code\":1,\"printedorder_id\":\"1654\",\"error_message\":\"\"}\""; | |
179 | - dd = dd.substring(1, dd.length()-1); | |
180 | - System.out.println(dd); | |
212 | +// String dd = "\"{\"success\":true,\"code\":1,\"printedorder_id\":\"1654\",\"error_message\":\"\"}\""; | |
213 | +// dd = dd.substring(1, dd.length()-1); | |
214 | +// System.out.println(dd); | |
181 | 215 | //JSONObject temp = JSONObject.fromObject(dd); |
182 | 216 | //System.out.println(temp.getInt("code")); |
217 | + | |
218 | + String trim = " sd ds sd "; | |
219 | + String trimLeft = " ds sd "; | |
220 | + String trimRight = " ds es &**^"; | |
221 | + System.out.println(trimByRegexW(trim)); | |
222 | + System.out.println(trimLeftByRegexW(trimLeft)); | |
223 | + System.out.println(trimRightByRegexW(trimRight)); | |
183 | 224 | } |
184 | 225 | } |
185 | 226 | \ No newline at end of file | ... | ... |