From 1b7c268bbe29eb46e432ea449bfac7c28c2e932b Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Sun, 22 May 2016 18:07:46 +0530 Subject: [PATCH] Fixed #35. Added support for `surround` function --- src/main/java/strman/Strman.java | 16 ++++++++++++++++ src/test/java/strman/StrmanTest.java | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index aec267f..616db25 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -961,6 +961,22 @@ public abstract class Strman { return result; } + + /** + * Surrounds a 'value' with the given 'prefix' and 'suffix'. + * + * @param value The input String + * @param prefix prefix. If suffix is null then prefix is used + * @param suffix suffix + * @return The String with surround substrs! + */ + public static String surround(final String value, final String prefix, final String suffix) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + String _prefix = Optional.ofNullable(prefix).orElse(""); + return append(_prefix, value, Optional.ofNullable(suffix).orElse(_prefix)); + } + + public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index f048b64..364c08a 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -711,6 +711,15 @@ public class StrmanTest { @Test public void transliterate_shouldTransliterateTheText() throws Exception { assertThat(transliterate("fóõ bár"), equalTo("foo bar")); + } + @Test + public void surround_shouldSurrondStringWithPrefixAndSuffix() throws Exception { + assertThat(surround("foo", "bar", null), equalTo("barfoobar")); + assertThat(surround("shekhar", "***", null), equalTo("***shekhar***")); + assertThat(surround("", ">", null), equalTo(">>")); + assertThat(surround("bar", "", null), equalTo("bar")); + assertThat(surround("f", null, null), equalTo("f")); + assertThat(surround("div", "<", ">"), equalTo("
")); } } \ No newline at end of file