/*** Take a string which are a delimited list and convert it to a string array. * <p>a Single delimiter can consists of more than one character:it would still * be considered as single delimit Er string, rather than as bunch of potential * delimiter characters-in contrast to {@codeTokenizetostringarray}. * @paramstr the input String *@paramdelimiter the delimiter between elements (this was a single delimiter, * rather than a bunch individual delimiter characters) *@paramCharstodelete A set of characters to delete. Useful for deleting unwanted * line breaks:e.g. "\r\n\f" would delete all new lines and line feeds in a String. * @returnAn array of the tokens in the list *@see#tokenizeToStringArray*/ Public Staticstring[] Delimitedlisttostringarray (String str, String delimiter, String charstodelete) {if(str = =NULL) { return NewString[0]; } if(Delimiter = =NULL) { return Newstring[] {str}; } List<String> result =NewArraylist<string>(); if("". Equals (delimiter)) { for(inti = 0; I < str.length (); i++) {Result.add (Deleteany (str.substring (i, I+ 1) , Charstodelete)); } } Else { intpos = 0; intDelpos; while((Delpos = Str.indexof (delimiter, pos))! =-1) {Result.add (Deleteany (str.substring (POS, Delpos), charstodelete)); POS= Delpos +delimiter.length (); } if(Str.length () > 0 && pos <=str.length ()) { //Add Rest of the String, but not in case of empty input.Result.add (Deleteany (str.substring (POS), charstodelete)); } } returnTostringarray (Result); } /*** Copy The given Collection into a String array. * The Collection must contain String elements only. * @paramCollection The collection to copy *@returnThe String Array ({@codenull} if the passed-in * Collection was {@codenull}) */ Public StaticString[] Tostringarray (collection<string>collection) { if(Collection = =NULL) { return NULL; } returnCollection.toarray (Newstring[collection.size ()]); } /*** Delete Any character in a given String. * @paraminstring the original String *@paramCharstodelete A set of characters to delete. * e.g. "az\n" would delete ' a ' s, ' z ' and new lines. * @returnThe resulting String*/ Public Staticstring Deleteany (String instring, String charstodelete) {if(!haslength (instring) | |!haslength (Charstodelete)) { returninstring; } StringBuilder SB=NewStringBuilder (); for(inti = 0; I < instring.length (); i++) { Charc =Instring.charat (i); if(Charstodelete.indexof (c) = =-1) {sb.append (c); } } returnsb.tostring (); } /*** Check The given String is neither {@codenull} Nor of length 0. * Note:will return {@codetrue} for a String, that purely consists of whitespace. * @paramstr the String to check ( could be {@codenull}) * @return {@codetrue if the String is not null and have length *@see#hasLength (charsequence)*/ Public Static Booleanhaslength (String str) {returnhaslength ((charsequence) str); } /*** Check The given charsequence is neither {@codenull} Nor of length 0. * Note:will return {@codetrue} for a charsequence that purely consists of whitespace. * <p><pre class= "code" > * STRINGUTILS.HASLENGTH (NULL) = False * Stringutils.haslength ("") = False * Stringutils.haslength ("") = True * Stringutils.haslength ("Hello") = True * </pre> *@paramstr the charsequence to check ( could be {@codenull}) * @return {@codetrue} if the charsequence is not null and have length *@see#hasText (String)*/ Public Static Booleanhaslength (charsequence str) {return(str! =NULL&& str.length () > 0); }
/*** Tokenize The given string into a string array via a stringtokenizer. * Trims tokens and omits empty tokens. * <p>the given delimiters string is supposed to consist of any number of * delimiter characters. Each of those characters can is used to separate * tokens. A delimiter is always a single character; For Multi-character * delimiters, consider using {@codeDelimitedlisttostringarray} *@paramstr the String to Tokenize *@paramdelimiters the delimiter characters, assembled as String * (each of those characters is individually considered a S delimiter). * @returnAn array of the tokens *@seeJava.util.StringTokenizer *@seeString#trim () *@see#delimitedListToStringArray*/ Public Staticstring[] Tokenizetostringarray (String str, string delimiters) {returnTokenizetostringarray (str, delimiters,true,true); } /*** Tokenize The given string into a string array via a stringtokenizer. * <p>the given delimiters string is supposed to consist of any number of * delimiter characters. Each of those characters can is used to separate * tokens. A delimiter is always a single character; For Multi-character * delimiters, consider using {@codeDelimitedlisttostringarray} *@paramstr the String to Tokenize *@paramdelimiters the delimiter characters, assembled as String * (each of those characters is individually considered a S delimiter) *@paramTrimtokens Trim the tokens via String ' s {@codeTrim} *@paramignoreemptytokens omit empty tokens from the result array * (only applies to tokens that is empty after trimming ; StringTokenizer * Won't consider subsequent delimiters as token in the first place). * @returnAn array of the tokens ({@codeNULL} If the input String * was {@codenull}) * @seeJava.util.StringTokenizer *@seeString#trim () *@see#delimitedListToStringArray*/ Public Staticstring[] Tokenizetostringarray (String str, string delimiters,BooleanTrimtokens,BooleanIgnoreemptytokens) { if(str = =NULL) { return NULL; } StringTokenizer St=NewStringTokenizer (str, delimiters); List<String> tokens =NewArraylist<string>(); while(St.hasmoretokens ()) {String token=St.nexttoken (); if(trimtokens) {token=Token.trim (); } if(!ignoreemptytokens | | token.length () > 0) {Tokens.add (token); } } returnTostringarray (tokens); } /*** Copy The given Collection into a String array. * The Collection must contain String elements only. * @paramCollection The collection to copy *@returnThe String Array ({@codenull} if the passed-in * Collection was {@codenull}) */ Public StaticString[] Tostringarray (collection<string>collection) { if(Collection = =NULL) { return NULL; } returnCollection.toarray (Newstring[collection.size ()]); }
Java Spring method for a type split