Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/StringUtils.java @ 875:5e9efdda6894
merged gnv-artifacts/1.0
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:56 +0200 |
parents | 2cea76f1112e |
children | f953c9a559d8 |
comparison
equal
deleted
inserted
replaced
722:bb3ffe7d719e | 875:5e9efdda6894 |
---|---|
1 package de.intevation.gnv.utils; | |
2 | |
3 /** | |
4 * Helper class which supports some methods for working with strings. | |
5 * | |
6 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
7 */ | |
8 public final class StringUtils | |
9 { | |
10 private StringUtils() { | |
11 } | |
12 | |
13 /** | |
14 * Append a string to a string array. | |
15 * | |
16 * @param haystack String array. | |
17 * @param straw String to append. | |
18 * @return the new string array. | |
19 */ | |
20 public static final String [] append(String [] haystack, String straw) { | |
21 if (haystack == null) { | |
22 return new String [] { straw }; | |
23 } | |
24 String [] nhaystack = new String[haystack.length + 1]; | |
25 System.arraycopy(haystack, 0, nhaystack, 0, haystack.length); | |
26 nhaystack[haystack.length] = straw; | |
27 return nhaystack; | |
28 } | |
29 | |
30 /** | |
31 * Checks the existence of a string in a given string array. | |
32 * | |
33 * @param haystack String array. | |
34 * @param needle String for being checked. | |
35 * @return true, if the string is contained in <i>haystack</i> - else false. | |
36 */ | |
37 public static final boolean contains(String [] haystack, String needle) { | |
38 if (haystack == null) { | |
39 return false; | |
40 } | |
41 | |
42 if (needle == null) { | |
43 for (int i = haystack.length - 1; i >= 0; --i) { | |
44 if (haystack[i] == null) { | |
45 return true; | |
46 } | |
47 } | |
48 } | |
49 else { | |
50 for (int i = haystack.length - 1; i >= 0; --i) { | |
51 String straw = haystack[i]; | |
52 if (straw != null && straw.equals(needle)) { | |
53 return true; | |
54 } | |
55 } | |
56 } | |
57 | |
58 return false; | |
59 } | |
60 } | |
61 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |