diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..9f084aa --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore index 48267e9..d6a4cf2 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,4 @@ hs_err_pid* !gradle/wrapper/gradle-wrapper.jar +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..0a4059d --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + strman + Project strman created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..03931c0 --- /dev/null +++ b/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,3 @@ +connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) +connection.project.dir= +eclipse.preferences.version=1 diff --git a/README.md b/README.md index a671ca8..9bca612 100644 --- a/README.md +++ b/README.md @@ -618,6 +618,19 @@ join(new String[]{"hello","world","123"}, ";") // result => "hello;world;123") ``` +## isBlank + +isEmpty returns true if a string is blank or null and false otherwise. + +```java +isBlank("") +// result => true) +isBlank(null) +// result => true) +isBlank("test") +// result => false) +``` + ## Inspiration This library is inspired by [dleitee/strman](https://github.com/dleitee/strman). diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 08ddd2b..70dc783 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -38,6 +38,9 @@ import static java.util.function.Function.identity; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static strman.Strman.isBlank; /** * A String manipulation library without any dependencies @@ -1305,6 +1308,16 @@ public abstract class Strman { .mapToObj(c -> (char) c) .collect(groupingBy(identity(), counting())); } + + /** + * Checks if string is empty. This is a null safe check and will return true when string is null. + * + * @param input The input string + * @return true if string is null or empty + */ + public static boolean isBlank(String input) { + return input == null || input.isEmpty(); + } private static void validate(String value, Predicate predicate, final Supplier supplier) { if (predicate.test(value)) { @@ -1325,5 +1338,6 @@ public abstract class Strman { } return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count); } + } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 84f18f6..a4fcc8b 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -37,8 +37,6 @@ import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; import static org.hamcrest.collection.IsArrayWithSize.emptyArray; import static org.junit.Assert.*; import static strman.Strman.*; -import static strman.Strman.endsWith; -import static strman.Strman.format; public class StrmanTest { @@ -1027,4 +1025,11 @@ public class StrmanTest { assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput)); } + + @Test + public void testIsBlank() { + assertTrue(isBlank("")); + assertTrue(isBlank(null)); + assertFalse(isBlank("ac")); + } } \ No newline at end of file