Spring Utils工具类常用方法实例
Spring提供的工具类,主要用于框架内部使用,这个类提供了一些简单的方法,并且提供了易于使用的方法在分割字符串,如CSV字符串,以及集合和数组。
StringUtils提供常用的方法如下:
判断对象对象是否为null或者空字符串
publicstaticbooleanisEmpty(@NullableObjectstr){ return(str==null||"".equals(str)); }
判断给的序列是否为空或者length为0
publicstaticbooleanhasLength(@NullableCharSequencestr){ return(str!=null&&str.length()>0); } publicstaticbooleanhasLength(@NullableStringstr){ return(str!=null&&!str.isEmpty()); }
判断字符串是否以某个字符串开头
publicstaticbooleanstartsWithIgnoreCase(@NullableStringstr,@NullableStringprefix){ return(str!=null&&prefix!=null&&str.length()>=prefix.length()&& str.regionMatches(true,0,prefix,0,prefix.length())); }
判断字符串是否以某个字符串结尾
publicstaticbooleanendsWithIgnoreCase(@NullableStringstr,@NullableStringsuffix){ return(str!=null&&suffix!=null&&str.length()>=suffix.length()&& str.regionMatches(true,str.length()-suffix.length(),suffix,0,suffix.length())); }
用另一个字符串替换字符串中出现的所有子字符串
publicstaticStringreplace(StringinString,StringoldPattern,@NullableStringnewPattern){ if(!hasLength(inString)||!hasLength(oldPattern)||newPattern==null){ returninString; } //oldPattern字符串第一次出现的位置 intindex=inString.indexOf(oldPattern); if(index==-1){ //nooccurrence->canreturninputas-is returninString; } //字符串长度 intcapacity=inString.length(); if(newPattern.length()>oldPattern.length()){ capacity+=16; } StringBuildersb=newStringBuilder(capacity); intpos=0;//ourpositionintheoldstring intpatLen=oldPattern.length(); while(index>=0){ sb.append(inString,pos,index); sb.append(newPattern); pos=index+patLen; index=inString.indexOf(oldPattern,pos); } //appendanycharacterstotherightofamatch sb.append(inString,pos,inString.length()); returnsb.toString(); }
根据给定的路径规范化路径
publicstaticStringcleanPath(Stringpath){ if(!hasLength(path)){ returnpath; } //用新字符串替换旧字符串 StringpathToUse=replace(path,WINDOWS_FOLDER_SEPARATOR,FOLDER_SEPARATOR); //Shortcutifthereisnoworktodo if(pathToUse.indexOf('.')==-1){ returnpathToUse; } //Stripprefixfrompathtoanalyze,tonottreatitaspartofthe //firstpathelement.Thisisnecessarytocorrectlyparsepathslike //"file:core/../core/io/Resource.class",wherethe".."shouldjust //stripthefirst"core"directorywhilekeepingthe"file:"prefix. intprefixIndex=pathToUse.indexOf(':'); Stringprefix=""; if(prefixIndex!=-1){ prefix=pathToUse.substring(0,prefixIndex+1); if(prefix.contains(FOLDER_SEPARATOR)){ prefix=""; } else{ pathToUse=pathToUse.substring(prefixIndex+1); } } if(pathToUse.startsWith(FOLDER_SEPARATOR)){ prefix=prefix+FOLDER_SEPARATOR; pathToUse=pathToUse.substring(1); } String[]pathArray=delimitedListToStringArray(pathToUse,FOLDER_SEPARATOR); LinkedListpathElements=newLinkedList<>(); inttops=0; for(inti=pathArray.length-1;i>=0;i--){ Stringelement=pathArray[i]; if(CURRENT_PATH.equals(element)){ //Pointstocurrentdirectory-dropit. } elseif(TOP_PATH.equals(element)){ //Registeringtoppathfound. tops++; } else{ if(tops>0){ //Mergingpathelementwithelementcorrespondingtotoppath. tops--; } else{ //Normalpathelementfound. pathElements.add(0,element); } } } //Remainingtoppathsneedtoberetained. for(inti=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。