DOC: StringUtils.containsAny. ADD: containsAllWords to WordUtils
This commit is contained in:
parent
1cb5573ada
commit
54facb4fd9
|
@ -1685,7 +1685,7 @@ public class StringUtils {
|
|||
* StringUtils.containsAny("", *) = false
|
||||
* StringUtils.containsAny(*, null) = false
|
||||
* StringUtils.containsAny(*, []) = false
|
||||
* StringUtils.containsAny("abcd", "ab", "cd") = false
|
||||
* StringUtils.containsAny("abcd", "ab", "cd") = true
|
||||
* StringUtils.containsAny("abc", "d", "abc") = true
|
||||
* </pre>
|
||||
*
|
||||
|
|
|
@ -16,9 +16,12 @@
|
|||
*/
|
||||
package org.apache.commons.lang3.text;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* <p>Operations on Strings that contain words.</p>
|
||||
*
|
||||
|
@ -562,6 +565,42 @@ public class WordUtils {
|
|||
return new String(buf, 0, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if the String contains all words in the given array.</p>
|
||||
*
|
||||
* <p>
|
||||
* A {@code null} String will return {@code false}. A {@code null, zero
|
||||
* length search array or if one element of array is null will return {@code false}.
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* WordUtils.containsAllWords(null, *) = false
|
||||
* WordUtils.containsAllWords("", *) = false
|
||||
* WordUtils.containsAllWords(*, null) = false
|
||||
* WordUtils.containsAllWords(*, []) = false
|
||||
* WordUtils.containsAllWords("abcd", "ab", "cd") = false
|
||||
* WordUtils.containsAllWords("abc def", "def", "abc") = true
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param str The str to check, may be null
|
||||
* @param words The array of String words to search for, may be null
|
||||
* @return {@code true} if all search words are found, {@code false} otherwise
|
||||
*/
|
||||
public static boolean containsAllWords(String word, String... words) {
|
||||
if (StringUtils.isEmpty(word) || ArrayUtils.isEmpty(words)
|
||||
|| (words.length == 1 && StringUtils.isBlank(words[0]))) {
|
||||
return false;
|
||||
}
|
||||
for (String w : words) {
|
||||
Pattern p = Pattern.compile(".*\\b" + w + "\\b.*");
|
||||
if (!p.matcher(word).matches()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Is the character a delimiter.
|
||||
|
|
|
@ -221,6 +221,25 @@ public class WordUtilsTest {
|
|||
assertEquals("I Am.fine", WordUtils.capitalizeFully("i am.fine", null) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAllWords_StringString() {
|
||||
assertFalse(WordUtils.containsAllWords(null, (String) null));
|
||||
assertFalse(WordUtils.containsAllWords(null, ""));
|
||||
assertFalse(WordUtils.containsAllWords(null, "ab"));
|
||||
|
||||
assertFalse(WordUtils.containsAllWords("", (String) null));
|
||||
assertFalse(WordUtils.containsAllWords("", ""));
|
||||
assertFalse(WordUtils.containsAllWords("", "ab"));
|
||||
|
||||
assertFalse(WordUtils.containsAllWords("foo", (String) null));
|
||||
assertFalse(WordUtils.containsAllWords("bar", ""));
|
||||
assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by"));
|
||||
assertTrue(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor"));
|
||||
assertFalse(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor"));
|
||||
assertFalse(WordUtils.containsAllWords("ab", "b"));
|
||||
assertFalse(WordUtils.containsAllWords("ab", "z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUncapitalize_String() {
|
||||
assertEquals(null, WordUtils.uncapitalize(null));
|
||||
|
|
Loading…
Reference in New Issue