2 * Copyright 2002-2008 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.osgi
.boot
.internal
.springutil
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Arrays
;
21 import java
.util
.Collection
;
22 import java
.util
.Collections
;
23 import java
.util
.Enumeration
;
24 import java
.util
.Iterator
;
25 import java
.util
.LinkedList
;
26 import java
.util
.List
;
27 import java
.util
.Locale
;
28 import java
.util
.Properties
;
30 import java
.util
.StringTokenizer
;
31 import java
.util
.TreeSet
;
34 * Miscellaneous {@link String} utility methods.
36 * <p>Mainly for internal use within the framework; consider
37 * <a href="http://jakarta.apache.org/commons/lang/">Jakarta's Commons Lang</a>
38 * for a more comprehensive suite of String utilities.
40 * <p>This class delivers some simple functionality that should really
41 * be provided by the core Java <code>String</code> and {@link StringBuffer}
42 * classes, such as the ability to {@link #replace} all occurrences of a given
43 * substring in a target string. It also provides easy-to-use methods to convert
44 * between delimited strings, such as CSV strings, and collections and arrays.
47 * @author Juergen Hoeller
48 * @author Keith Donald
51 * @since 16 April 2001
52 * @see org.apache.commons.lang.StringUtils
54 public abstract class StringUtils
{
56 private static final String FOLDER_SEPARATOR
= "/";
58 private static final String WINDOWS_FOLDER_SEPARATOR
= "\\";
60 private static final String TOP_PATH
= "..";
62 private static final String CURRENT_PATH
= ".";
64 private static final char EXTENSION_SEPARATOR
= '.';
67 //---------------------------------------------------------------------
68 // General convenience methods for working with Strings
69 //---------------------------------------------------------------------
72 * Check that the given CharSequence is neither <code>null</code> nor of length 0.
73 * Note: Will return <code>true</code> for a CharSequence that purely consists of whitespace.
75 * StringUtils.hasLength(null) = false
76 * StringUtils.hasLength("") = false
77 * StringUtils.hasLength(" ") = true
78 * StringUtils.hasLength("Hello") = true
80 * @param str the CharSequence to check (may be <code>null</code>)
81 * @return <code>true</code> if the CharSequence is not null and has length
82 * @see #hasText(String)
84 public static boolean hasLength(CharSequence str
) {
85 return (str
!= null && str
.length() > 0);
89 * Check that the given String is neither <code>null</code> nor of length 0.
90 * Note: Will return <code>true</code> for a String that purely consists of whitespace.
91 * @param str the String to check (may be <code>null</code>)
92 * @return <code>true</code> if the String is not null and has length
93 * @see #hasLength(CharSequence)
95 public static boolean hasLength(String str
) {
96 return hasLength((CharSequence
) str
);
100 * Check whether the given CharSequence has actual text.
101 * More specifically, returns <code>true</code> if the string not <code>null</code>,
102 * its length is greater than 0, and it contains at least one non-whitespace character.
104 * StringUtils.hasText(null) = false
105 * StringUtils.hasText("") = false
106 * StringUtils.hasText(" ") = false
107 * StringUtils.hasText("12345") = true
108 * StringUtils.hasText(" 12345 ") = true
110 * @param str the CharSequence to check (may be <code>null</code>)
111 * @return <code>true</code> if the CharSequence is not <code>null</code>,
112 * its length is greater than 0, and it does not contain whitespace only
113 * @see java.lang.Character#isWhitespace
115 public static boolean hasText(CharSequence str
) {
116 if (!hasLength(str
)) {
119 int strLen
= str
.length();
120 for (int i
= 0; i
< strLen
; i
++) {
121 if (!Character
.isWhitespace(str
.charAt(i
))) {
129 * Check whether the given String has actual text.
130 * More specifically, returns <code>true</code> if the string not <code>null</code>,
131 * its length is greater than 0, and it contains at least one non-whitespace character.
132 * @param str the String to check (may be <code>null</code>)
133 * @return <code>true</code> if the String is not <code>null</code>, its length is
134 * greater than 0, and it does not contain whitespace only
135 * @see #hasText(CharSequence)
137 public static boolean hasText(String str
) {
138 return hasText((CharSequence
) str
);
142 * Check whether the given CharSequence contains any whitespace characters.
143 * @param str the CharSequence to check (may be <code>null</code>)
144 * @return <code>true</code> if the CharSequence is not empty and
145 * contains at least 1 whitespace character
146 * @see java.lang.Character#isWhitespace
148 public static boolean containsWhitespace(CharSequence str
) {
149 if (!hasLength(str
)) {
152 int strLen
= str
.length();
153 for (int i
= 0; i
< strLen
; i
++) {
154 if (Character
.isWhitespace(str
.charAt(i
))) {
162 * Check whether the given String contains any whitespace characters.
163 * @param str the String to check (may be <code>null</code>)
164 * @return <code>true</code> if the String is not empty and
165 * contains at least 1 whitespace character
166 * @see #containsWhitespace(CharSequence)
168 public static boolean containsWhitespace(String str
) {
169 return containsWhitespace((CharSequence
) str
);
173 * Trim leading and trailing whitespace from the given String.
174 * @param str the String to check
175 * @return the trimmed String
176 * @see java.lang.Character#isWhitespace
178 public static String
trimWhitespace(String str
) {
179 if (!hasLength(str
)) {
182 StringBuffer buf
= new StringBuffer(str
);
183 while (buf
.length() > 0 && Character
.isWhitespace(buf
.charAt(0))) {
186 while (buf
.length() > 0 && Character
.isWhitespace(buf
.charAt(buf
.length() - 1))) {
187 buf
.deleteCharAt(buf
.length() - 1);
189 return buf
.toString();
193 * Trim <i>all</i> whitespace from the given String:
194 * leading, trailing, and inbetween characters.
195 * @param str the String to check
196 * @return the trimmed String
197 * @see java.lang.Character#isWhitespace
199 public static String
trimAllWhitespace(String str
) {
200 if (!hasLength(str
)) {
203 StringBuffer buf
= new StringBuffer(str
);
205 while (buf
.length() > index
) {
206 if (Character
.isWhitespace(buf
.charAt(index
))) {
207 buf
.deleteCharAt(index
);
213 return buf
.toString();
217 * Trim leading whitespace from the given String.
218 * @param str the String to check
219 * @return the trimmed String
220 * @see java.lang.Character#isWhitespace
222 public static String
trimLeadingWhitespace(String str
) {
223 if (!hasLength(str
)) {
226 StringBuffer buf
= new StringBuffer(str
);
227 while (buf
.length() > 0 && Character
.isWhitespace(buf
.charAt(0))) {
230 return buf
.toString();
234 * Trim trailing whitespace from the given String.
235 * @param str the String to check
236 * @return the trimmed String
237 * @see java.lang.Character#isWhitespace
239 public static String
trimTrailingWhitespace(String str
) {
240 if (!hasLength(str
)) {
243 StringBuffer buf
= new StringBuffer(str
);
244 while (buf
.length() > 0 && Character
.isWhitespace(buf
.charAt(buf
.length() - 1))) {
245 buf
.deleteCharAt(buf
.length() - 1);
247 return buf
.toString();
251 * Trim all occurences of the supplied leading character from the given String.
252 * @param str the String to check
253 * @param leadingCharacter the leading character to be trimmed
254 * @return the trimmed String
256 public static String
trimLeadingCharacter(String str
, char leadingCharacter
) {
257 if (!hasLength(str
)) {
260 StringBuffer buf
= new StringBuffer(str
);
261 while (buf
.length() > 0 && buf
.charAt(0) == leadingCharacter
) {
264 return buf
.toString();
268 * Trim all occurences of the supplied trailing character from the given String.
269 * @param str the String to check
270 * @param trailingCharacter the trailing character to be trimmed
271 * @return the trimmed String
273 public static String
trimTrailingCharacter(String str
, char trailingCharacter
) {
274 if (!hasLength(str
)) {
277 StringBuffer buf
= new StringBuffer(str
);
278 while (buf
.length() > 0 && buf
.charAt(buf
.length() - 1) == trailingCharacter
) {
279 buf
.deleteCharAt(buf
.length() - 1);
281 return buf
.toString();
286 * Test if the given String starts with the specified prefix,
287 * ignoring upper/lower case.
288 * @param str the String to check
289 * @param prefix the prefix to look for
290 * @see java.lang.String#startsWith
292 public static boolean startsWithIgnoreCase(String str
, String prefix
) {
293 if (str
== null || prefix
== null) {
296 if (str
.startsWith(prefix
)) {
299 if (str
.length() < prefix
.length()) {
302 String lcStr
= str
.substring(0, prefix
.length()).toLowerCase();
303 String lcPrefix
= prefix
.toLowerCase();
304 return lcStr
.equals(lcPrefix
);
308 * Test if the given String ends with the specified suffix,
309 * ignoring upper/lower case.
310 * @param str the String to check
311 * @param suffix the suffix to look for
312 * @see java.lang.String#endsWith
314 public static boolean endsWithIgnoreCase(String str
, String suffix
) {
315 if (str
== null || suffix
== null) {
318 if (str
.endsWith(suffix
)) {
321 if (str
.length() < suffix
.length()) {
325 String lcStr
= str
.substring(str
.length() - suffix
.length()).toLowerCase();
326 String lcSuffix
= suffix
.toLowerCase();
327 return lcStr
.equals(lcSuffix
);
331 * Test whether the given string matches the given substring
332 * at the given index.
333 * @param str the original string (or StringBuffer)
334 * @param index the index in the original string to start matching against
335 * @param substring the substring to match at the given index
337 public static boolean substringMatch(CharSequence str
, int index
, CharSequence substring
) {
338 for (int j
= 0; j
< substring
.length(); j
++) {
340 if (i
>= str
.length() || str
.charAt(i
) != substring
.charAt(j
)) {
348 * Count the occurrences of the substring in string s.
349 * @param str string to search in. Return 0 if this is null.
350 * @param sub string to search for. Return 0 if this is null.
352 public static int countOccurrencesOf(String str
, String sub
) {
353 if (str
== null || sub
== null || str
.length() == 0 || sub
.length() == 0) {
356 int count
= 0, pos
= 0, idx
= 0;
357 while ((idx
= str
.indexOf(sub
, pos
)) != -1) {
359 pos
= idx
+ sub
.length();
365 * Replace all occurences of a substring within a string with
367 * @param inString String to examine
368 * @param oldPattern String to replace
369 * @param newPattern String to insert
370 * @return a String with the replacements
372 public static String
replace(String inString
, String oldPattern
, String newPattern
) {
373 if (!hasLength(inString
) || !hasLength(oldPattern
) || newPattern
== null) {
376 StringBuffer sbuf
= new StringBuffer();
377 // output StringBuffer we'll build up
378 int pos
= 0; // our position in the old string
379 int index
= inString
.indexOf(oldPattern
);
380 // the index of an occurrence we've found, or -1
381 int patLen
= oldPattern
.length();
383 sbuf
.append(inString
.substring(pos
, index
));
384 sbuf
.append(newPattern
);
385 pos
= index
+ patLen
;
386 index
= inString
.indexOf(oldPattern
, pos
);
388 sbuf
.append(inString
.substring(pos
));
389 // remember to append any characters to the right of a match
390 return sbuf
.toString();
394 * Delete all occurrences of the given substring.
395 * @param inString the original String
396 * @param pattern the pattern to delete all occurrences of
397 * @return the resulting String
399 public static String
delete(String inString
, String pattern
) {
400 return replace(inString
, pattern
, "");
404 * Delete any character in a given String.
405 * @param inString the original String
406 * @param charsToDelete a set of characters to delete.
407 * E.g. "az\n" will delete 'a's, 'z's and new lines.
408 * @return the resulting String
410 public static String
deleteAny(String inString
, String charsToDelete
) {
411 if (!hasLength(inString
) || !hasLength(charsToDelete
)) {
414 StringBuffer out
= new StringBuffer();
415 for (int i
= 0; i
< inString
.length(); i
++) {
416 char c
= inString
.charAt(i
);
417 if (charsToDelete
.indexOf(c
) == -1) {
421 return out
.toString();
425 //---------------------------------------------------------------------
426 // Convenience methods for working with formatted Strings
427 //---------------------------------------------------------------------
430 * Quote the given String with single quotes.
431 * @param str the input String (e.g. "myString")
432 * @return the quoted String (e.g. "'myString'"),
433 * or <code>null<code> if the input was <code>null</code>
435 public static String
quote(String str
) {
436 return (str
!= null ?
"'" + str
+ "'" : null);
440 * Turn the given Object into a String with single quotes
441 * if it is a String; keeping the Object as-is else.
442 * @param obj the input Object (e.g. "myString")
443 * @return the quoted String (e.g. "'myString'"),
444 * or the input object as-is if not a String
446 public static Object
quoteIfString(Object obj
) {
447 return (obj
instanceof String ?
quote((String
) obj
) : obj
);
451 * Unqualify a string qualified by a '.' dot character. For example,
452 * "this.name.is.qualified", returns "qualified".
453 * @param qualifiedName the qualified name
455 public static String
unqualify(String qualifiedName
) {
456 return unqualify(qualifiedName
, '.');
460 * Unqualify a string qualified by a separator character. For example,
461 * "this:name:is:qualified" returns "qualified" if using a ':' separator.
462 * @param qualifiedName the qualified name
463 * @param separator the separator
465 public static String
unqualify(String qualifiedName
, char separator
) {
466 return qualifiedName
.substring(qualifiedName
.lastIndexOf(separator
) + 1);
470 * Capitalize a <code>String</code>, changing the first letter to
471 * upper case as per {@link Character#toUpperCase(char)}.
472 * No other letters are changed.
473 * @param str the String to capitalize, may be <code>null</code>
474 * @return the capitalized String, <code>null</code> if null
476 public static String
capitalize(String str
) {
477 return changeFirstCharacterCase(str
, true);
481 * Uncapitalize a <code>String</code>, changing the first letter to
482 * lower case as per {@link Character#toLowerCase(char)}.
483 * No other letters are changed.
484 * @param str the String to uncapitalize, may be <code>null</code>
485 * @return the uncapitalized String, <code>null</code> if null
487 public static String
uncapitalize(String str
) {
488 return changeFirstCharacterCase(str
, false);
491 private static String
changeFirstCharacterCase(String str
, boolean capitalize
) {
492 if (str
== null || str
.length() == 0) {
495 StringBuffer buf
= new StringBuffer(str
.length());
497 buf
.append(Character
.toUpperCase(str
.charAt(0)));
500 buf
.append(Character
.toLowerCase(str
.charAt(0)));
502 buf
.append(str
.substring(1));
503 return buf
.toString();
507 * Extract the filename from the given path,
508 * e.g. "mypath/myfile.txt" -> "myfile.txt".
509 * @param path the file path (may be <code>null</code>)
510 * @return the extracted filename, or <code>null</code> if none
512 public static String
getFilename(String path
) {
516 int separatorIndex
= path
.lastIndexOf(FOLDER_SEPARATOR
);
517 return (separatorIndex
!= -1 ? path
.substring(separatorIndex
+ 1) : path
);
521 * Extract the filename extension from the given path,
522 * e.g. "mypath/myfile.txt" -> "txt".
523 * @param path the file path (may be <code>null</code>)
524 * @return the extracted filename extension, or <code>null</code> if none
526 public static String
getFilenameExtension(String path
) {
530 int sepIndex
= path
.lastIndexOf(EXTENSION_SEPARATOR
);
531 return (sepIndex
!= -1 ? path
.substring(sepIndex
+ 1) : null);
535 * Strip the filename extension from the given path,
536 * e.g. "mypath/myfile.txt" -> "mypath/myfile".
537 * @param path the file path (may be <code>null</code>)
538 * @return the path with stripped filename extension,
539 * or <code>null</code> if none
541 public static String
stripFilenameExtension(String path
) {
545 int sepIndex
= path
.lastIndexOf(EXTENSION_SEPARATOR
);
546 return (sepIndex
!= -1 ? path
.substring(0, sepIndex
) : path
);
550 * Apply the given relative path to the given path,
551 * assuming standard Java folder separation (i.e. "/" separators);
552 * @param path the path to start from (usually a full file path)
553 * @param relativePath the relative path to apply
554 * (relative to the full file path above)
555 * @return the full file path that results from applying the relative path
557 public static String
applyRelativePath(String path
, String relativePath
) {
558 int separatorIndex
= path
.lastIndexOf(FOLDER_SEPARATOR
);
559 if (separatorIndex
!= -1) {
560 String newPath
= path
.substring(0, separatorIndex
);
561 if (!relativePath
.startsWith(FOLDER_SEPARATOR
)) {
562 newPath
+= FOLDER_SEPARATOR
;
564 return newPath
+ relativePath
;
572 * Normalize the path by suppressing sequences like "path/.." and
574 * <p>The result is convenient for path comparison. For other uses,
575 * notice that Windows separators ("\") are replaced by simple slashes.
576 * @param path the original path
577 * @return the normalized path
579 public static String
cleanPath(String path
) {
583 String pathToUse
= replace(path
, WINDOWS_FOLDER_SEPARATOR
, FOLDER_SEPARATOR
);
585 // Strip prefix from path to analyze, to not treat it as part of the
586 // first path element. This is necessary to correctly parse paths like
587 // "file:core/../core/io/Resource.class", where the ".." should just
588 // strip the first "core" directory while keeping the "file:" prefix.
589 int prefixIndex
= pathToUse
.indexOf(":");
591 if (prefixIndex
!= -1) {
592 prefix
= pathToUse
.substring(0, prefixIndex
+ 1);
593 pathToUse
= pathToUse
.substring(prefixIndex
+ 1);
595 if (pathToUse
.startsWith(FOLDER_SEPARATOR
)) {
596 prefix
= prefix
+ FOLDER_SEPARATOR
;
597 pathToUse
= pathToUse
.substring(1);
600 String
[] pathArray
= delimitedListToStringArray(pathToUse
, FOLDER_SEPARATOR
);
601 List pathElements
= new LinkedList();
604 for (int i
= pathArray
.length
- 1; i
>= 0; i
--) {
605 String element
= pathArray
[i
];
606 if (CURRENT_PATH
.equals(element
)) {
607 // Points to current directory - drop it.
609 else if (TOP_PATH
.equals(element
)) {
610 // Registering top path found.
615 // Merging path element with element corresponding to top path.
619 // Normal path element found.
620 pathElements
.add(0, element
);
625 // Remaining top paths need to be retained.
626 for (int i
= 0; i
< tops
; i
++) {
627 pathElements
.add(0, TOP_PATH
);
630 return prefix
+ collectionToDelimitedString(pathElements
, FOLDER_SEPARATOR
);
634 * Compare two paths after normalization of them.
635 * @param path1 first path for comparison
636 * @param path2 second path for comparison
637 * @return whether the two paths are equivalent after normalization
639 public static boolean pathEquals(String path1
, String path2
) {
640 return cleanPath(path1
).equals(cleanPath(path2
));
644 * Parse the given <code>localeString</code> into a {@link Locale}.
645 * <p>This is the inverse operation of {@link Locale#toString Locale's toString}.
646 * @param localeString the locale string, following <code>Locale's</code>
647 * <code>toString()</code> format ("en", "en_UK", etc);
648 * also accepts spaces as separators, as an alternative to underscores
649 * @return a corresponding <code>Locale</code> instance
651 public static Locale
parseLocaleString(String localeString
) {
652 String
[] parts
= tokenizeToStringArray(localeString
, "_ ", false, false);
653 String language
= (parts
.length
> 0 ? parts
[0] : "");
654 String country
= (parts
.length
> 1 ? parts
[1] : "");
656 if (parts
.length
>= 2) {
657 // There is definitely a variant, and it is everything after the country
658 // code sans the separator between the country code and the variant.
659 int endIndexOfCountryCode
= localeString
.indexOf(country
) + country
.length();
660 // Strip off any leading '_' and whitespace, what's left is the variant.
661 variant
= trimLeadingWhitespace(localeString
.substring(endIndexOfCountryCode
));
662 if (variant
.startsWith("_")) {
663 variant
= trimLeadingCharacter(variant
, '_');
666 return (language
.length() > 0 ?
new Locale(language
, country
, variant
) : null);
670 * Determine the RFC 3066 compliant language tag,
671 * as used for the HTTP "Accept-Language" header.
672 * @param locale the Locale to transform to a language tag
673 * @return the RFC 3066 compliant language tag as String
675 public static String
toLanguageTag(Locale locale
) {
676 return locale
.getLanguage() + (hasText(locale
.getCountry()) ?
"-" + locale
.getCountry() : "");
680 //---------------------------------------------------------------------
681 // Convenience methods for working with String arrays
682 //---------------------------------------------------------------------
685 * Append the given String to the given String array, returning a new array
686 * consisting of the input array contents plus the given String.
687 * @param array the array to append to (can be <code>null</code>)
688 * @param str the String to append
689 * @return the new array (never <code>null</code>)
691 public static String
[] addStringToArray(String
[] array
, String str
) {
692 if (ObjectUtils
.isEmpty(array
)) {
693 return new String
[] {str
};
695 String
[] newArr
= new String
[array
.length
+ 1];
696 System
.arraycopy(array
, 0, newArr
, 0, array
.length
);
697 newArr
[array
.length
] = str
;
702 * Concatenate the given String arrays into one,
703 * with overlapping array elements included twice.
704 * <p>The order of elements in the original arrays is preserved.
705 * @param array1 the first array (can be <code>null</code>)
706 * @param array2 the second array (can be <code>null</code>)
707 * @return the new array (<code>null</code> if both given arrays were <code>null</code>)
709 public static String
[] concatenateStringArrays(String
[] array1
, String
[] array2
) {
710 if (ObjectUtils
.isEmpty(array1
)) {
713 if (ObjectUtils
.isEmpty(array2
)) {
716 String
[] newArr
= new String
[array1
.length
+ array2
.length
];
717 System
.arraycopy(array1
, 0, newArr
, 0, array1
.length
);
718 System
.arraycopy(array2
, 0, newArr
, array1
.length
, array2
.length
);
723 * Merge the given String arrays into one, with overlapping
724 * array elements only included once.
725 * <p>The order of elements in the original arrays is preserved
726 * (with the exception of overlapping elements, which are only
727 * included on their first occurence).
728 * @param array1 the first array (can be <code>null</code>)
729 * @param array2 the second array (can be <code>null</code>)
730 * @return the new array (<code>null</code> if both given arrays were <code>null</code>)
732 public static String
[] mergeStringArrays(String
[] array1
, String
[] array2
) {
733 if (ObjectUtils
.isEmpty(array1
)) {
736 if (ObjectUtils
.isEmpty(array2
)) {
739 List result
= new ArrayList();
740 result
.addAll(Arrays
.asList(array1
));
741 for (int i
= 0; i
< array2
.length
; i
++) {
742 String str
= array2
[i
];
743 if (!result
.contains(str
)) {
747 return toStringArray(result
);
751 * Turn given source String array into sorted array.
752 * @param array the source array
753 * @return the sorted array (never <code>null</code>)
755 public static String
[] sortStringArray(String
[] array
) {
756 if (ObjectUtils
.isEmpty(array
)) {
757 return new String
[0];
764 * Copy the given Collection into a String array.
765 * The Collection must contain String elements only.
766 * @param collection the Collection to copy
767 * @return the String array (<code>null</code> if the passed-in
768 * Collection was <code>null</code>)
770 public static String
[] toStringArray(Collection collection
) {
771 if (collection
== null) {
774 return (String
[]) collection
.toArray(new String
[collection
.size()]);
778 * Copy the given Enumeration into a String array.
779 * The Enumeration must contain String elements only.
780 * @param enumeration the Enumeration to copy
781 * @return the String array (<code>null</code> if the passed-in
782 * Enumeration was <code>null</code>)
784 public static String
[] toStringArray(Enumeration enumeration
) {
785 if (enumeration
== null) {
788 List list
= Collections
.list(enumeration
);
789 return (String
[]) list
.toArray(new String
[list
.size()]);
793 * Trim the elements of the given String array,
794 * calling <code>String.trim()</code> on each of them.
795 * @param array the original String array
796 * @return the resulting array (of the same size) with trimmed elements
798 public static String
[] trimArrayElements(String
[] array
) {
799 if (ObjectUtils
.isEmpty(array
)) {
800 return new String
[0];
802 String
[] result
= new String
[array
.length
];
803 for (int i
= 0; i
< array
.length
; i
++) {
804 String element
= array
[i
];
805 result
[i
] = (element
!= null ? element
.trim() : null);
811 * Remove duplicate Strings from the given array.
812 * Also sorts the array, as it uses a TreeSet.
813 * @param array the String array
814 * @return an array without duplicates, in natural sort order
816 public static String
[] removeDuplicateStrings(String
[] array
) {
817 if (ObjectUtils
.isEmpty(array
)) {
820 Set set
= new TreeSet();
821 for (int i
= 0; i
< array
.length
; i
++) {
824 return toStringArray(set
);
828 * Split a String at the first occurrence of the delimiter.
829 * Does not include the delimiter in the result.
830 * @param toSplit the string to split
831 * @param delimiter to split the string up with
832 * @return a two element array with index 0 being before the delimiter, and
833 * index 1 being after the delimiter (neither element includes the delimiter);
834 * or <code>null</code> if the delimiter wasn't found in the given input String
836 public static String
[] split(String toSplit
, String delimiter
) {
837 if (!hasLength(toSplit
) || !hasLength(delimiter
)) {
840 int offset
= toSplit
.indexOf(delimiter
);
844 String beforeDelimiter
= toSplit
.substring(0, offset
);
845 String afterDelimiter
= toSplit
.substring(offset
+ delimiter
.length());
846 return new String
[] {beforeDelimiter
, afterDelimiter
};
850 * Take an array Strings and split each element based on the given delimiter.
851 * A <code>Properties</code> instance is then generated, with the left of the
852 * delimiter providing the key, and the right of the delimiter providing the value.
853 * <p>Will trim both the key and value before adding them to the
854 * <code>Properties</code> instance.
855 * @param array the array to process
856 * @param delimiter to split each element using (typically the equals symbol)
857 * @return a <code>Properties</code> instance representing the array contents,
858 * or <code>null</code> if the array to process was null or empty
860 public static Properties
splitArrayElementsIntoProperties(String
[] array
, String delimiter
) {
861 return splitArrayElementsIntoProperties(array
, delimiter
, null);
865 * Take an array Strings and split each element based on the given delimiter.
866 * A <code>Properties</code> instance is then generated, with the left of the
867 * delimiter providing the key, and the right of the delimiter providing the value.
868 * <p>Will trim both the key and value before adding them to the
869 * <code>Properties</code> instance.
870 * @param array the array to process
871 * @param delimiter to split each element using (typically the equals symbol)
872 * @param charsToDelete one or more characters to remove from each element
873 * prior to attempting the split operation (typically the quotation mark
874 * symbol), or <code>null</code> if no removal should occur
875 * @return a <code>Properties</code> instance representing the array contents,
876 * or <code>null</code> if the array to process was <code>null</code> or empty
878 public static Properties
splitArrayElementsIntoProperties(
879 String
[] array
, String delimiter
, String charsToDelete
) {
881 if (ObjectUtils
.isEmpty(array
)) {
884 Properties result
= new Properties();
885 for (int i
= 0; i
< array
.length
; i
++) {
886 String element
= array
[i
];
887 if (charsToDelete
!= null) {
888 element
= deleteAny(array
[i
], charsToDelete
);
890 String
[] splittedElement
= split(element
, delimiter
);
891 if (splittedElement
== null) {
894 result
.setProperty(splittedElement
[0].trim(), splittedElement
[1].trim());
900 * Tokenize the given String into a String array via a StringTokenizer.
901 * Trims tokens and omits empty tokens.
902 * <p>The given delimiters string is supposed to consist of any number of
903 * delimiter characters. Each of those characters can be used to separate
904 * tokens. A delimiter is always a single character; for multi-character
905 * delimiters, consider using <code>delimitedListToStringArray</code>
906 * @param str the String to tokenize
907 * @param delimiters the delimiter characters, assembled as String
908 * (each of those characters is individually considered as delimiter).
909 * @return an array of the tokens
910 * @see java.util.StringTokenizer
911 * @see java.lang.String#trim()
912 * @see #delimitedListToStringArray
914 public static String
[] tokenizeToStringArray(String str
, String delimiters
) {
915 return tokenizeToStringArray(str
, delimiters
, true, true);
919 * Tokenize the given String into a String array via a StringTokenizer.
920 * <p>The given delimiters string is supposed to consist of any number of
921 * delimiter characters. Each of those characters can be used to separate
922 * tokens. A delimiter is always a single character; for multi-character
923 * delimiters, consider using <code>delimitedListToStringArray</code>
924 * @param str the String to tokenize
925 * @param delimiters the delimiter characters, assembled as String
926 * (each of those characters is individually considered as delimiter)
927 * @param trimTokens trim the tokens via String's <code>trim</code>
928 * @param ignoreEmptyTokens omit empty tokens from the result array
929 * (only applies to tokens that are empty after trimming; StringTokenizer
930 * will not consider subsequent delimiters as token in the first place).
931 * @return an array of the tokens (<code>null</code> if the input String
932 * was <code>null</code>)
933 * @see java.util.StringTokenizer
934 * @see java.lang.String#trim()
935 * @see #delimitedListToStringArray
937 public static String
[] tokenizeToStringArray(
938 String str
, String delimiters
, boolean trimTokens
, boolean ignoreEmptyTokens
) {
943 StringTokenizer st
= new StringTokenizer(str
, delimiters
);
944 List tokens
= new ArrayList();
945 while (st
.hasMoreTokens()) {
946 String token
= st
.nextToken();
948 token
= token
.trim();
950 if (!ignoreEmptyTokens
|| token
.length() > 0) {
954 return toStringArray(tokens
);
958 * Take a String which is a delimited list and convert it to a String array.
959 * <p>A single delimiter can consists of more than one character: It will still
960 * be considered as single delimiter string, rather than as bunch of potential
961 * delimiter characters - in contrast to <code>tokenizeToStringArray</code>.
962 * @param str the input String
963 * @param delimiter the delimiter between elements (this is a single delimiter,
964 * rather than a bunch individual delimiter characters)
965 * @return an array of the tokens in the list
966 * @see #tokenizeToStringArray
968 public static String
[] delimitedListToStringArray(String str
, String delimiter
) {
969 return delimitedListToStringArray(str
, delimiter
, null);
973 * Take a String which is a delimited list and convert it to a String array.
974 * <p>A single delimiter can consists of more than one character: It will still
975 * be considered as single delimiter string, rather than as bunch of potential
976 * delimiter characters - in contrast to <code>tokenizeToStringArray</code>.
977 * @param str the input String
978 * @param delimiter the delimiter between elements (this is a single delimiter,
979 * rather than a bunch individual delimiter characters)
980 * @param charsToDelete a set of characters to delete. Useful for deleting unwanted
981 * line breaks: e.g. "\r\n\f" will delete all new lines and line feeds in a String.
982 * @return an array of the tokens in the list
983 * @see #tokenizeToStringArray
985 public static String
[] delimitedListToStringArray(String str
, String delimiter
, String charsToDelete
) {
987 return new String
[0];
989 if (delimiter
== null) {
990 return new String
[] {str
};
992 List result
= new ArrayList();
993 if ("".equals(delimiter
)) {
994 for (int i
= 0; i
< str
.length(); i
++) {
995 result
.add(deleteAny(str
.substring(i
, i
+ 1), charsToDelete
));
1001 while ((delPos
= str
.indexOf(delimiter
, pos
)) != -1) {
1002 result
.add(deleteAny(str
.substring(pos
, delPos
), charsToDelete
));
1003 pos
= delPos
+ delimiter
.length();
1005 if (str
.length() > 0 && pos
<= str
.length()) {
1006 // Add rest of String, but not in case of empty input.
1007 result
.add(deleteAny(str
.substring(pos
), charsToDelete
));
1010 return toStringArray(result
);
1014 * Convert a CSV list into an array of Strings.
1015 * @param str the input String
1016 * @return an array of Strings, or the empty array in case of empty input
1018 public static String
[] commaDelimitedListToStringArray(String str
) {
1019 return delimitedListToStringArray(str
, ",");
1023 * Convenience method to convert a CSV string list to a set.
1024 * Note that this will suppress duplicates.
1025 * @param str the input String
1026 * @return a Set of String entries in the list
1028 public static Set
commaDelimitedListToSet(String str
) {
1029 Set set
= new TreeSet();
1030 String
[] tokens
= commaDelimitedListToStringArray(str
);
1031 for (int i
= 0; i
< tokens
.length
; i
++) {
1038 * Convenience method to return a Collection as a delimited (e.g. CSV)
1039 * String. E.g. useful for <code>toString()</code> implementations.
1040 * @param coll the Collection to display
1041 * @param delim the delimiter to use (probably a ",")
1042 * @param prefix the String to start each element with
1043 * @param suffix the String to end each element with
1044 * @return the delimited String
1046 public static String
collectionToDelimitedString(Collection coll
, String delim
, String prefix
, String suffix
) {
1047 if (CollectionUtils
.isEmpty(coll
)) {
1050 StringBuffer sb
= new StringBuffer();
1051 Iterator it
= coll
.iterator();
1052 while (it
.hasNext()) {
1053 sb
.append(prefix
).append(it
.next()).append(suffix
);
1058 return sb
.toString();
1062 * Convenience method to return a Collection as a delimited (e.g. CSV)
1063 * String. E.g. useful for <code>toString()</code> implementations.
1064 * @param coll the Collection to display
1065 * @param delim the delimiter to use (probably a ",")
1066 * @return the delimited String
1068 public static String
collectionToDelimitedString(Collection coll
, String delim
) {
1069 return collectionToDelimitedString(coll
, delim
, "", "");
1073 * Convenience method to return a Collection as a CSV String.
1074 * E.g. useful for <code>toString()</code> implementations.
1075 * @param coll the Collection to display
1076 * @return the delimited String
1078 public static String
collectionToCommaDelimitedString(Collection coll
) {
1079 return collectionToDelimitedString(coll
, ",");
1083 * Convenience method to return a String array as a delimited (e.g. CSV)
1084 * String. E.g. useful for <code>toString()</code> implementations.
1085 * @param arr the array to display
1086 * @param delim the delimiter to use (probably a ",")
1087 * @return the delimited String
1089 public static String
arrayToDelimitedString(Object
[] arr
, String delim
) {
1090 if (ObjectUtils
.isEmpty(arr
)) {
1093 StringBuffer sb
= new StringBuffer();
1094 for (int i
= 0; i
< arr
.length
; i
++) {
1100 return sb
.toString();
1104 * Convenience method to return a String array as a CSV String.
1105 * E.g. useful for <code>toString()</code> implementations.
1106 * @param arr the array to display
1107 * @return the delimited String
1109 public static String
arrayToCommaDelimitedString(Object
[] arr
) {
1110 return arrayToDelimitedString(arr
, ",");