12. Android框架和工具之 StringUtils(字符串操作)
1. StringUtils介绍:
StringUtils是apache commons lang库(http://commons.apache.org/proper/commons-lang/download_lang.cgi)旗下的一个工具类,提供了很多有用的处理字符串的方法。
关于StringUtils方法全集,可以看看这篇博客:http://blog.sina.com.cn/s/blog_4550f3ca0100qrsd.html
2. 这里我们主要介绍StringUtils常用的方法,如下:
(1)新建一个Java工程,如下:
(2)来到代码中,如下:
1 package com.himi.stringutils; 2 3 import org.apache.commons.lang3.StringUtils; 4 5 /** 6 * StringUtils常用方法 7 * 8 * @author hebao 9 * 10 */ 11 public class StringUtilsTest { 12 public static void main(String[] args) { 13 14 /** 15 * 检查字符串是否为空: 16 * static boolean isEmpty(CharSequence cs):判断字符串是否为空或null; 17 * static boolean isNotEmpty(CharSequence str):判断字符串是否非空或非null; 18 * 19 * static boolean isBlank(CharSequence str):判断字符串是否为空或null; 20 * static boolean isNotBlank(CharSequence str):判断字符串是否非空或非null; 21 */ 22 String str = "happy"; 23 System.out.println(StringUtils.isEmpty(str));//false 24 System.out.println(StringUtils.isNotEmpty(str));//true 25 System.out.println(StringUtils.isBlank(str));//false 26 System.out.println(StringUtils.isNotBlank(str));//true 27 28 29 /** 30 * 清除字符串首尾空白字符: 31 * static String trim(String str):清除掉str首尾的空白字符,str为空或者清除之后为空,return null 32 * static String trimToEmpty(String str):清除掉str首尾的空白字符,str为空或者清除之后为空,return "" 33 * static String trimToNull(String str):清除掉str首尾的空白字符,str为空或者清除之后为空,return "" 34 * 35 * static String strip(String str):清除掉str首尾的空白字符 36 * static String strip(String str, String stripChars): 去掉 str 两端的在 stripChars中的字符 37 * static String[] stripAll(String... strs):删除字符串数组strs中所有字符串元素首尾的空白字符 38 * static String[] stripAll(String[] strs, String stripChars):去掉字符串数组strs中所有字符串元素首尾在 stripChars中的字符 39 * 40 * static String stripEnd(String str, String stripChars):去掉 str末尾的在 stripChars中的字符 41 * static String stripStart(String str, String stripChars):去掉 str首部的在 stripChars中的字符 42 */ 43 String str1 = " !@ha32@@@prry! @$$ "; 44 String str2 = "ab23323232baewuyacb"; 45 String[] strs1 = new String[] {" abc ","aecwver89ewfac", 46 "123321fsgreg"," 214g5499 0efw@#32"}; 47 System.out.println(StringUtils.trim(str1));//!@ha32@@@prry! @$$ 48 System.out.println(StringUtils.trimToEmpty(str1));//!@ha32@@@prry! @$$ 49 System.out.println(StringUtils.trimToNull(str1));//!@ha32@@@prry! @$$ 50 51 System.out.println(StringUtils.strip(str1));//!@ha32@@@prry! @$$ 52 System.out.println(StringUtils.strip(str2,"abc"));//23323232baewuy 53 54 //[abc,aecwver89ewfac,123321fsgreg,214g5499 0efw@#32] 55 System.out.println(printArray(StringUtils.stripAll(strs1))); 56 //[,ecwver89ewf,fsgreg,4g5499 0efw@#] 57 System.out.println(printArray(StringUtils.stripAll(strs1," abc123"))); 58 59 //ab23323232baewuy 60 System.out.println(StringUtils.stripEnd(str2,"abc123")); 61 //ewuyacb 62 System.out.println(StringUtils.stripStart(str2,"abc123")); 63 64 /** 65 * 查找字符串是否包含其他字符: 66 * 67 * 查找字符串seq中是否包含字符串searchSeq 68 * static boolean contains(CharSequence seq, CharSequence searchSeq) 69 * 查找字符串seq中是否包含字符searchChar 70 * static boolean contains(CharSequence seq, int searchChar) 71 * 72 * 查找字符串seq中是否包含任何在字符数组searchChars中的字符 73 * static boolean containsAny(CharSequence cs, char... searchChars) 74 * 查找字符串seq中是否包含任何在字符串数组searchChars中的字符串 75 * static boolean containsAny(CharSequence cs, CharSequence... searchCharSequences) 76 * 查找字符串seq中是否包含字符串searchChars 77 * static boolean containsAny(CharSequence cs, CharSequence searchChars) 78 * 79 * 查找字符串seq中是否包含字符串searchChars,忽略大小写 80 * static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr) 81 * 82 * 查找字符串cs中是否不包含任何在字符数组searchChars中的字符 83 * static boolean containsNone(CharSequence cs, char... searchChars) 84 * 查找字符串cs中是否不包含特定字符invalidChars(不能包含invalidChars任何字符) 85 * static boolean containsNone(CharSequence cs, String invalidChars) 86 * 87 * 查找字符串cs中是否只是由字符数组valid中的字符组成的(不一定用到所有字符) 88 * static boolean containsOnly(CharSequence cs, char... valid) 89 * static boolean containsOnly(CharSequence cs, String validChars) 90 * 91 * 查找字符串seq是否包含空格字符 92 * static boolean containsWhitespace(CharSequence seq) 93 */ 94 95 96 97 /** 98 * 比较两个字符串的不同: 99 * static String difference(String str1,String str2): 100 * 返回值:the portion of str2 where it differs from str1; 101 */ 102 System.out.println(StringUtils.difference("", "abc"));//abc 103 System.out.println(StringUtils.difference("ab", "abxyz"));//xyz 104 System.out.println(StringUtils.difference("abcde", "xyz"));//xyz 105 106 /** 107 * 删除字符串中的所有空格: 108 * static String deleteWhitespace(String str) 109 */ 110 //0wfewfefwff88few 111 System.out.println(StringUtils.deleteWhitespace(" 0wfewf efwff88 few ")); 112 113 114 /** 115 * 判断字符串的前缀: 116 * static boolean startsWith(CharSequence str, CharSequence prefix) 117 * static boolean startsWithAny(CharSequence string, CharSequence... searchStrings) 118 * static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix) 119 */ 120 System.out.println(StringUtils.startsWith("Img01.jpg","Img"));//true 121 System.out.println(StringUtils.startsWithAny("abcxyz", 122 new String[] {null, "xyz", "abc"}));//true 123 124 /** 125 * 判断字符串的后缀: 126 * static boolean endsWith(CharSequence str, CharSequence suffix) 127 * static boolean endsWithAny(CharSequence string, CharSequence... searchStrings) 128 * static boolean endsWithIgnoreCase(CharSequence str, CharSequence suffix) 129 */ 130 System.out.println(StringUtils.endsWith("fly.mp4",".mp4"));//true 131 System.out.println(StringUtils.endsWithAny("abcxyz", new String[] {""}));//true 132 System.out.println(StringUtils.endsWithAny("abcxyz", 133 new String[] {null, "xyz", "abc"}));//true 134 System.out.println(StringUtils.endsWith("fly.mp4",".MP4"));//false 135 136 137 /** 138 * 判断两个字符串的是否相等: 139 * static boolean equals(CharSequence cs1, CharSequence cs2) 140 * static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) 141 */ 142 System.out.println(StringUtils.equals("AaBbCcDd","aabbccdd"));//false 143 System.out.println(StringUtils.equalsIgnoreCase("AaBbCcDd","aabbccdd"));//true 144 145 146 /** 147 * 字符串的索引: 148 * static int indexOf(CharSequence seq, CharSequence searchSeq) 149 * static int indexOf(CharSequence seq, CharSequence searchSeq, int startPos) 150 * static int indexOf(CharSequence seq, int searchChar) 151 * static int indexOf(CharSequence seq, int searchChar, int startPos) 152 * 153 * 搜索特定字符串在目标字符串CharSequence中首次出现的索引 154 * static int indexOfAny(CharSequence cs, char... searchChars) 155 * static int indexOfAny(CharSequence str, CharSequence... searchStrs) 156 * static int indexOfAny(CharSequence cs, String searchChars) 157 * 158 * static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr) 159 * static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) 160 * 161 */ 162 System.out.println(StringUtils.indexOf("abcdefg123rg","fg"));//5 163 System.out.println(StringUtils.indexOf("abcdefg123rg",'e'));//4 164 //System.out.println(StringUtils.indexOfAny("zzabyycdxx", "za"));//0 165 System.out.println(StringUtils.indexOfAny("zzabyycdxx", "ab"));//2 166 System.out.println(StringUtils.indexOfIgnoreCase("zzabyycdxx", "ZA"));//1 167 168 169 /** 170 * 检查字符串是否全是大写或者小写字符组成: 171 * static boolean isAllLowerCase(CharSequence cs) 172 * static boolean isAllUpperCase(CharSequence cs) 173 */ 174 System.out.println(StringUtils.isAllUpperCase("ABCDEFG"));//true 175 System.out.println(StringUtils.isAllLowerCase("AbCdEfG"));//false 176 177 178 /** 179 * 检查字符串是否是字符、字母、字母和数字组成的: 180 * isAlpha(CharSequence cs) 181 * isNumeric(CharSequence cs) 182 * isAlphanumeric(CharSequence cs) 183 */ 184 System.out.println(StringUtils.isAlpha("Afdasfdsf"));//true 185 System.out.println(StringUtils.isNumeric("Afdasfdsf123123"));//false 186 System.out.println(StringUtils.isAlphanumeric("Afdasfdsf123123"));//true 187 188 /** 189 * 方法用于把数组中的所有元素放入一个字符串。 元素是通过指定的分隔符进行分隔的: 190 * static String join(byte[] array, char separator) 191 * static String join(byte[] array, char separator, int startIndex, int endIndex) 192 * static String join(char[] array, char separator) 193 * static String join(char[] array, char separator, int startIndex, int endIndex) 194 * static String join(double[] array, char separator) 195 * static String join(double[] array, char separator, int startIndex, int endIndex) 196 * .... 197 * static String join(Object[] array, char separator) 198 * static String join(Object[] array, char separator, int startIndex, int endIndex) 199 * static String join(Object[] array, String separator) 200 * static String join(Object[] array, String separator, int startIndex, int endIndex) 201 * 202 */ 203 //ab;bde;23fe 204 System.out.println(StringUtils.join(new String[]{"ab","bde","23fe"}, ';')); 205 //12.45&23.4&45.3 206 System.out.println(StringUtils.join(new float[]{12.45f,23.4f,45.3f}, '&')); 207 208 209 210 /** 211 * 去除字符串中特定字符 或者 字符串: 212 * static String remove(String str, char remove) 213 * static String remove(String str, String remove) 214 * 215 * 如果str字符串末尾是remove字符串,就删除str末尾的remove,否则返回str 216 * static String removeEnd(String str, String remove) 217 * static String removeEndIgnoreCase(String str, String remove) 218 * 219 * 如果str字符串开始是remove字符串,就删除str开始的remove,否则返回str 220 * static String removeStart(String str, String remove) 221 * static String removeStartIgnoreCase(String str, String remove) 222 */ 223 //www.domain.com 224 System.out.println(StringUtils.removeStart("www.domain.com", "domain")); 225 //domain.com 226 System.out.println(StringUtils.removeStart("www.domain.com", "www.")); 227 //www.domain.com 228 System.out.println(StringUtils.removeEnd("www.domain.com", "domain")); 229 //www.domain 230 System.out.println(StringUtils.removeEnd("www.domain.com", ".com")); 231 232 233 234 /** 235 * 重复字符串次数: 236 * static String repeat(char ch, int repeat) 237 * static String repeat(String str, int repeat) 238 */ 239 //LoveLoveLove 240 System.out.println(StringUtils.repeat("Love", 3)); 241 242 243 244 /** 245 * 字符串替换: 246 * static String replace(String text, String searchString, String replacement) 247 * 参数解析: 248 * text---搜索的文本 249 * searchString---在text文本中搜索的目标字符串searchString 250 * replacement---用来替换searchString 251 * 252 * static String replace(String text, String searchString, String replacement, int max) 253 * static String replaceChars(String str, char searchChar, char replaceChar) 254 * static String replaceChars(String str, String searchChars, String replaceChars) 255 */ 256 //I ♥ YinChang 257 System.out.println(StringUtils.replace("I Love YinChang", "Love","♥")); 258 259 260 /** 261 * 字符串的反转: 262 * static String reverse(String str) 263 * 264 */ 265 //I Love YinChang 266 System.out.println(StringUtils.reverse("gnahCniY evoL I")); 267 268 269 /** 270 * 字符串分割成字符串数组: 271 * static String[] split(String str)//默认使用空格作为分离标记 272 * static String[] split(String str, char separatorChar) 273 * static String[] split(String str, String separatorChars) 274 */ 275 //[HeBao,is,Good,Boy] 276 System.out.println(printArray(StringUtils.split("HeBao is Good Boy"))); 277 //[HeBao,is,Good,Boy] 278 System.out.println(printArray(StringUtils.split("HeBao#@is#@Good#@Boy","#@"))); 279 280 281 282 /** 283 * 切割字符串: 284 * static String substring(String str, int start) 285 * 286 * start<= X <end 287 * static String substring(String str, int start, int end) 288 * 289 * 290 * static String substringAfter(String str, String separator) 291 * 截取str中最后出现的分离符separator后面的字符串 292 * static String substringAfterLast(String str, String separator) 293 * 294 * 295 * static String substringBefore(String str, String separator) 296 * 截取str中最后出现的分离符separator前面的字符串 297 * static String substringBeforeLast(String str, String separator) 298 * 299 * 300 * static String substringBetween(String str, String tag) 301 * static String substringBetween(String str, String open, String close) 302 * static String substringsBetween(String str, String open, String close) 303 */ 304 //Love 305 System.out.println(StringUtils.substring("hebaoLove",5)); 306 //hebao 307 System.out.println(StringUtils.substring("hebaoLove",0,5)); 308 309 //Love 310 System.out.println(StringUtils.substringAfter("hebaoLove","o")); 311 //ve 312 System.out.println(StringUtils.substringAfterLast("hebaoLove","o")); 313 314 //abc 315 System.out.println(StringUtils.substringBetween("tagabctag", "tag")); 316 //abc 317 System.out.println(StringUtils.substringBetween("yabczyabcz", "y", "z")); 318 } 319 320 public static String printArray(String[] strs) { 321 StringBuffer sb = new StringBuffer(); 322 sb.append("["); 323 for(int i=0; i<strs.length; i++) { 324 if(i==strs.length-1) { 325 sb.append(strs[i]+"]"); 326 break; 327 } 328 sb.append(strs[i]+","); 329 } 330 331 return sb.toString(); 332 333 } 334 335 }