itoccuredtometodaythebehaviorofjavastring.split () is very strange。
Actually I want to split a string 'aa,bb,cc,dd,ee' to array by .split (,',',') that gives me a string array [ ],]
But when I try to split a String 'aa,bb,cc,dd,' toarraythisgivesmearrayoflength4means only [ ' aa ',' bb ',' cc
iwantaprocedurethatsplitsastringlike ' aa,bb,cc,dd,' aa ',' bb ',' cc ',',' dd ',' to aray
isthispossiblewithjava.lang.stringapi? 高级thanks in。
解决方案
' aa、bb、cc、dd、'.split (、'、-1) ) )。
whenstring.split(stringregex ) is called,it is called with limit=0,whichwillremovealltrailingemptystringsinthearray ) imith
theactualbehaviorofstring.split (字符串注册) is quite confusing:
splittinganemptystringwillresultinanarrayoflength1. emptystringsplitwillalwaysresultinlength1arraycontaingtheemptystring。
Splitting ';' 或'; ) )。 with regex being '; ' willresultinanemptyarray.non-emptystringsplitwillresultinalltrailingemptystringsinthearrayremoved。
thebehaviorabovecanbeobservedfromatleastjava5to Java 8。
therewasanattempttochangethebehaviortoreturnanemptyarraywhensplittinganemptystringinjdk-6559590.however, itwassoonrevertedinjdk-8028321 whenitcausesregressioninvariousplaces.thechangenevermakesitintotheinitialjava 8发行版。