Commit b1ff109aeb30108c7e4185a84a64fbb34abe5f45
1 parent
090b511c
Exists in
master
1. CODE_PAGE 和 NO_BREAK_SPACE处理优化
Showing
2 changed files
with
95 additions
and
13 deletions
Show diff stats
build.gradle
| @@ -59,7 +59,7 @@ uploadArchives { | @@ -59,7 +59,7 @@ uploadArchives { | ||
| 59 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) | 59 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
| 60 | } | 60 | } |
| 61 | pom.project { | 61 | pom.project { |
| 62 | - version '1.1.107' | 62 | + version '1.1.110' |
| 63 | artifactId ARTIFACT_Id | 63 | artifactId ARTIFACT_Id |
| 64 | groupId GROUP_ID | 64 | groupId GROUP_ID |
| 65 | packaging TYPE | 65 | packaging TYPE |
src/main/java/com/taover/util/UtilString.java
| @@ -8,6 +8,11 @@ import java.util.regex.Matcher; | @@ -8,6 +8,11 @@ import java.util.regex.Matcher; | ||
| 8 | import java.util.regex.Pattern; | 8 | import java.util.regex.Pattern; |
| 9 | 9 | ||
| 10 | public class UtilString { | 10 | public class UtilString { |
| 11 | + public static final byte[] CODE_PAGE_BYTE_ARR = new byte[]{-30, -128, -83}; | ||
| 12 | + public static final String CODE_PAGE = new String(CODE_PAGE_BYTE_ARR); | ||
| 13 | + public static final byte[] NO_BREAK_SPACE_BYTE_ARR = new byte[]{-62, -96}; | ||
| 14 | + public static final String NO_BREAK_SPACE = new String(NO_BREAK_SPACE_BYTE_ARR); | ||
| 15 | + | ||
| 11 | public static String trimByRegexS(String source){ | 16 | public static String trimByRegexS(String source){ |
| 12 | return trimLeftByRegexS(trimRightByRegexS(source)); | 17 | return trimLeftByRegexS(trimRightByRegexS(source)); |
| 13 | } | 18 | } |
| @@ -20,8 +25,8 @@ public class UtilString { | @@ -20,8 +25,8 @@ public class UtilString { | ||
| 20 | return ""; | 25 | return ""; |
| 21 | } | 26 | } |
| 22 | 27 | ||
| 23 | - source = replaceCodePage(source, ""); | ||
| 24 | - source = replaceNoBreakBackspace(source, ""); | 28 | + source = trimLeftCodePage(source); |
| 29 | + source = trimLeftNoBreakBackspace(source); | ||
| 25 | Pattern pattern = Pattern.compile("\\S"); | 30 | Pattern pattern = Pattern.compile("\\S"); |
| 26 | int startIndex = -1; | 31 | int startIndex = -1; |
| 27 | for(int i=0; i<source.length(); ++i){ | 32 | for(int i=0; i<source.length(); ++i){ |
| @@ -46,8 +51,8 @@ public class UtilString { | @@ -46,8 +51,8 @@ public class UtilString { | ||
| 46 | return ""; | 51 | return ""; |
| 47 | } | 52 | } |
| 48 | 53 | ||
| 49 | - source = replaceCodePage(source, ""); | ||
| 50 | - source = replaceNoBreakBackspace(source, ""); | 54 | + source = trimRightCodePage(source); |
| 55 | + source = trimNoBreakBackspace(source); | ||
| 51 | Pattern pattern = Pattern.compile("\\S"); | 56 | Pattern pattern = Pattern.compile("\\S"); |
| 52 | int endIndex = source.length()-1; | 57 | int endIndex = source.length()-1; |
| 53 | for(int i=source.length()-1; i>=0; --i){ | 58 | for(int i=source.length()-1; i>=0; --i){ |
| @@ -96,18 +101,94 @@ public class UtilString { | @@ -96,18 +101,94 @@ public class UtilString { | ||
| 96 | } | 101 | } |
| 97 | return ""; | 102 | return ""; |
| 98 | } | 103 | } |
| 104 | + | ||
| 105 | + public static String trimCodePage(String data) { | ||
| 106 | + return trimLeftCodePage(trimRightCodePage(data)); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + public static String trimLeftCodePage(String data) { | ||
| 110 | + return trimLeftStrByEquals(data, CODE_PAGE); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + public static String trimRightCodePage(String data) { | ||
| 114 | + return trimRightStrByEquals(data, CODE_PAGE); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + public static String trimNoBreakBackspace(String data) { | ||
| 118 | + return trimLeftNoBreakBackspace(trimRightNoBreakBackspace(data)); | ||
| 119 | + } | ||
| 99 | 120 | ||
| 100 | - @Deprecated | ||
| 101 | - public static String trimCodePage(String data){ | ||
| 102 | - return data.replaceAll(new String(new byte[]{-30, -128, -83}), ""); | 121 | + public static String trimLeftNoBreakBackspace(String data) { |
| 122 | + return trimLeftStrByEquals(data, NO_BREAK_SPACE); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + public static String trimRightNoBreakBackspace(String data) { | ||
| 126 | + return trimRightStrByEquals(data, NO_BREAK_SPACE); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + public static String trimLeftStrByRegex(String data, Pattern trimPattern) { | ||
| 130 | + /** | ||
| 131 | + * TO DO | ||
| 132 | + */ | ||
| 133 | + return null; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + public static String trimRightStrByRegex(String data, Pattern trimPattern) { | ||
| 137 | + /** | ||
| 138 | + * TO DO | ||
| 139 | + */ | ||
| 140 | + return null; | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + public static String trimLeftStrByEquals(String data, String trimStr) { | ||
| 144 | + if(data == null){ | ||
| 145 | + return null; | ||
| 146 | + } | ||
| 147 | + if(data.equals("")){ | ||
| 148 | + return ""; | ||
| 149 | + } | ||
| 150 | + if(trimStr == null || trimStr.isEmpty()) { | ||
| 151 | + return data; | ||
| 152 | + } | ||
| 153 | + int subIndexBegin = 0; | ||
| 154 | + int trimStrLen = trimStr.length(); | ||
| 155 | + int dataLen = data.length(); | ||
| 156 | + while((subIndexBegin+trimStrLen) <= dataLen) { | ||
| 157 | + if(!trimStr.equals(data.substring(subIndexBegin, subIndexBegin+trimStrLen))) { | ||
| 158 | + break; | ||
| 159 | + } | ||
| 160 | + subIndexBegin += trimStrLen; | ||
| 161 | + } | ||
| 162 | + return data.substring(subIndexBegin); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + public static String trimRightStrByEquals(String data, String trimStr) { | ||
| 166 | + if(data == null){ | ||
| 167 | + return null; | ||
| 168 | + } | ||
| 169 | + if(data.equals("")){ | ||
| 170 | + return ""; | ||
| 171 | + } | ||
| 172 | + if(trimStr == null || trimStr.isEmpty()) { | ||
| 173 | + return data; | ||
| 174 | + } | ||
| 175 | + int subIndexEnd = data.length(); | ||
| 176 | + int trimStrLen = trimStr.length(); | ||
| 177 | + while((subIndexEnd - trimStrLen) >= 0) { | ||
| 178 | + if(!trimStr.equals(data.substring(subIndexEnd-trimStrLen, subIndexEnd))) { | ||
| 179 | + break; | ||
| 180 | + } | ||
| 181 | + subIndexEnd -= trimStrLen; | ||
| 182 | + } | ||
| 183 | + return data.substring(0, subIndexEnd); | ||
| 103 | } | 184 | } |
| 104 | 185 | ||
| 105 | public static String replaceCodePage(String data, String replaceStr){ | 186 | public static String replaceCodePage(String data, String replaceStr){ |
| 106 | - return data.replaceAll(new String(new byte[]{-30, -128, -83}), replaceStr); | 187 | + return data.replaceAll(CODE_PAGE, replaceStr); |
| 107 | } | 188 | } |
| 108 | 189 | ||
| 109 | public static String replaceNoBreakBackspace(String data, String replaceStr) { | 190 | public static String replaceNoBreakBackspace(String data, String replaceStr) { |
| 110 | - return data.replaceAll(new String(new byte[] {-62, -96}), replaceStr); | 191 | + return data.replaceAll(NO_BREAK_SPACE, replaceStr); |
| 111 | } | 192 | } |
| 112 | 193 | ||
| 113 | /** | 194 | /** |
| @@ -284,8 +365,9 @@ public class UtilString { | @@ -284,8 +365,9 @@ public class UtilString { | ||
| 284 | String trim = " sd ds sd "; | 365 | String trim = " sd ds sd "; |
| 285 | String trimLeft = " ds sd "; | 366 | String trimLeft = " ds sd "; |
| 286 | String trimRight = " ds es &**^"; | 367 | String trimRight = " ds es &**^"; |
| 287 | - System.out.println(trimByRegexW(trim)); | ||
| 288 | - System.out.println(trimLeftByRegexW(trimLeft)); | ||
| 289 | - System.out.println(trimRightByRegexW(trimRight)); | 368 | + System.out.println(trimByRegexS(trim)); |
| 369 | + System.out.println(trimLeftByRegexS(trimLeft)); | ||
| 370 | + System.out.println(trimRightByRegexS(trimRight)); | ||
| 371 | + System.out.println(trimRightStrByEquals("asddasd", "asd")); | ||
| 290 | } | 372 | } |
| 291 | } | 373 | } |
| 292 | \ No newline at end of file | 374 | \ No newline at end of file |