1212public interface Strman {
1313
1414 Predicate <String > NULL_STRING_PREDICATE = str -> str == null ;
15- Predicate <String > EMPTY_STRING_PREDICATE = str -> str .length () == 0 ;
16- Predicate <String > NULL_AND_EMPTY_STRING_PREDICATE = NULL_STRING_PREDICATE .and (EMPTY_STRING_PREDICATE );
17- String EMPTY_STRING = " " ;
15+ Supplier <String > NULL_STRING_MSG_SUPPLIER = () -> "'value' should be not null." ;
1816
1917 /**
2018 * Appends Strings to value
@@ -35,7 +33,7 @@ static String append(final String value, final String... appends) {
3533 * @return full String
3634 */
3735 static String appendArray (final String value , final String [] appends ) {
38- validate (value , NULL_STRING_PREDICATE , () -> "'value' should be not null." );
36+ validate (value , NULL_STRING_PREDICATE , NULL_STRING_MSG_SUPPLIER );
3937 if (appends == null || appends .length == 0 ) {
4038 return value ;
4139 }
@@ -69,14 +67,14 @@ static Optional<String> at(final String value, int index) {
6967 /**
7068 * Returns an array with strings between start and end.
7169 *
72- * @param value
73- * @param start
74- * @param end
70+ * @param value input
71+ * @param start start
72+ * @param end end
7573 * @return Array containing different parts between start and end.
7674 */
7775
7876 static String [] between (final String value , final String start , final String end ) {
79- validate (value , NULL_STRING_PREDICATE , () -> "'value' should be not null." );
77+ validate (value , NULL_STRING_PREDICATE , NULL_STRING_MSG_SUPPLIER );
8078 validate (start , NULL_STRING_PREDICATE , () -> "'start' should be not null." );
8179 validate (end , NULL_STRING_PREDICATE , () -> "'end' should be not null." );
8280
@@ -95,7 +93,7 @@ static String[] chars(final String value) {
9593 * The other implementation of this could be using String's split method
9694 * String[] chars = value.split("")
9795 */
98- validate (value , NULL_STRING_PREDICATE , () -> "'value' should be not null." );
96+ validate (value , NULL_STRING_PREDICATE , NULL_STRING_MSG_SUPPLIER );
9997 int length = value .length ();
10098 String [] result = new String [length ];
10199 for (int i = 0 ; i < length ; i ++) {
@@ -112,13 +110,42 @@ static String[] chars(final String value) {
112110 * @return collapsed String
113111 */
114112 static String collapseWhitespace (final String value ) {
115- validate (value , NULL_STRING_PREDICATE , () -> "'value' should be not null." );
113+ validate (value , NULL_STRING_PREDICATE , NULL_STRING_MSG_SUPPLIER );
116114 return value .trim ().replaceAll ("\\ s\\ s+" , " " );
117115 }
118116
117+
119118 static void validate (String value , Predicate <String > predicate , final Supplier <String > supplier ) {
120119 if (predicate .test (value )) {
121120 throw new IllegalArgumentException (supplier .get ());
122121 }
123122 }
123+
124+ /**
125+ * Verifies that the needle is contained in the value. The search is case insensitive
126+ *
127+ * @param value to search
128+ * @param needle to find
129+ * @return true if found else false.
130+ */
131+ static boolean contains (final String value , final String needle ) {
132+ return contains (value , needle , false );
133+ }
134+
135+ /**
136+ *
137+ * Verifies that the needle is contained in the value.
138+ *
139+ * @param value to search
140+ * @param needle to find
141+ * @param caseSensitive true or false
142+ * @return true if found else false.
143+ */
144+ static boolean contains (final String value , final String needle , final boolean caseSensitive ) {
145+ validate (value , NULL_STRING_PREDICATE , NULL_STRING_MSG_SUPPLIER );
146+ if (caseSensitive ) {
147+ return value .indexOf (needle ) > -1 ;
148+ }
149+ return value .toLowerCase ().indexOf (needle .toLowerCase ()) > -1 ;
150+ }
124151}
0 commit comments