-
Notifications
You must be signed in to change notification settings - Fork 360
Home
A Java 8 library for working with String. You can refer to Javadocs online http://shekhargulati.github.io/strman-java/.
To use strman in your application, you have to add strman
in your classpath. strman is available on Maven Central so you just need to add dependency to your favorite build tool as show below.
For Apache Maven users, please add following to your pom.xml.
<dependencies>
<dependency>
<groupId>com.shekhargulati</groupId>
<artifactId>strman</artifactId>
<version>0.4.0</version>
</dependency>
</dependencies>
Gradle users can add following to their build.gradle file.
compile(group: 'com.shekhargulati', name: 'strman', version: '0.4.0')
To learn what we added in the latest version please refer to ./changelog.md
.
These are the available functions in current version of library:
Appends Strings to value
import static strman.Strman.append
append("f", "o", "o", "b", "a", "r")
// result => "foobar"
Append an array of String to value
import static strman.Strman.appendArray
appendArray("f", new String[]{"o", "o", "b", "a", "r"}
// result => "foobar"
Get the character at index. This method will take care of negative indexes.
import static strman.Strman.at
at("foobar", 0)
// result => Optional("f")
Returns an array with strings between start and end.
import static strman.Strman.between
between("[abc][def]", "[", "]")
// result => ["abc","def"]
Returns a String array consisting of the characters in the String.
import static strman.Strman.chars
chars("title")
// result => ["t", "i", "t", "l", "e"]
Counts the number of occurrences of each character in the string
import static strman.Strman.charsCount
charsCount("abca")
// result => Map(("a",2),("b",1),("c",1))
Replace consecutive whitespace characters with a single space.
import static strman.Strman.collapseWhitespace
collapseWhitespace("foo bar")
// result => "foo bar"
Verifies that the needle is contained in the value.
import static strman.Strman.contains
contains("foo bar","foo")
// result => true
contains("foo bar","FOO", false) // turning off case sensitivity
// result => true
Verifies that all needles are contained in value
import static strman.Strman.containsAll
containsAll("foo bar", new String[]{"foo", "bar"})
// result => true
containsAll("foo bar", new String[]{"FOO", "bar"},false)
// result => true
Verifies that one or more of needles are contained in value.
import static strman.Strman.containsAny
containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
// result => true
Count the number of times substr appears in value
import static strman.Strman.countSubstr
countSubstr("aaaAAAaaa", "aaa")
// result => 2
countSubstr("aaaAAAaaa", "aaa", false, false)
// result => 3
Test if value ends with search.
import static strman.Strman.endsWith
endsWith("foo bar", "bar")
// result => true
endsWith("foo Bar", "BAR", false)
// result => true
Ensures that the value begins with prefix. If it doesn't exist, it's prepended.
import static strman.Strman.ensureLeft
ensureLeft("foobar", "foo")
// result => "foobar"
ensureLeft("bar", "foo")
// result => "foobar"
ensureLeft("foobar", "FOO", false)
// result => "foobar"
Decodes data encoded with MIME base64
import static strman.Strman.base64Decode
base64Decode("c3RybWFu")
// result => "strman"
Encodes data with MIME base64.
import static strman.Strman.base64Encode
base64Encode("strman")
// result => "c3RybWFu"
Convert binary unicode (16 digits) string to string chars
import static strman.Strman.binDecode
binDecode("0000000001000001")
// result => "A"
Convert string chars to binary unicode (16 digits)
import static strman.Strman.binEncode
binEncode("A")
// result => "0000000001000001"
Convert decimal unicode (5 digits) string to string chars
import static strman.Strman.decDecode
decDecode("00065")
// result => "A"
Convert string chars to decimal unicode (5 digits)
import static strman.Strman.decEncode
decEncode("A")
// result => "00065"
Ensures that the value ends with suffix. If it doesn't, it's appended.
import static strman.Strman.ensureRight
ensureRight("foo", "bar")
// result => "foobar"
ensureRight("foobar", "bar")
// result => "foobar"
ensureRight("fooBAR", "bar", false)
// result => "foobar"
Returns the first n chars of String
import static strman.Strman.first
first("foobar", 3)
// result => "foo"
Return the first char of String
import static strman.Strman.head
head("foobar")
// result => "f"
Convert hexadecimal unicode (4 digits) string to string chars
import static strman.Strman.hexDecode
hexDecode("0041")
// result => "A"
Convert string chars to hexadecimal unicode (4 digits)
import static strman.Strman.hexEncode
hexEncode("A")
// result => "0041"
Tests if two Strings are inequal
import static strman.Strman.inequal
inequal("a", "b")
// result => true
Inserts 'substr' into the 'value' at the 'index' provided.
import static strman.Strman.insert
insert("fbar", "oo", 1)
// result => "foobar"
Return the last n chars of String
import static strman.Strman.last
last("foobarfoo", 3)
// result => "foo"
Returns a new string of a given length such that the beginning of the string is padded.
import static strman.Strman.leftPad
leftPad("1", "0", 5)
// result => "00001"
This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset.
import static strman.Strman.lastIndexOf
lastIndexOf("foobarfoobar", "F", false)
// result => 6
Removes all spaces on left
import static strman.Strman.leftTrim
leftTrim(" strman")
// result => "strman"
Return a new String starting with prepends
prepend("r", "f", "o", "o", "b", "a")
// "foobar"
Remove empty Strings from string array
removeEmptyStrings(new String[]{"aa", "", " ", "bb", "cc", null})
// result => ["aa", "bb", "cc"]
Returns a new String with the prefix removed, if present.
removeLeft("foofoo", "foo")
// "foo"
Remove all non word characters.
removeNonWords("foo&bar-")
// result => "foobar"
Returns a new string with the 'suffix' removed, if present.
removeRight("foobar", "bar")
// result => "foo"
removeRight("foobar", "BAR",false)
// result => "foo"
Remove all spaces and replace for value.
removeSpaces("foo bar")
// result => "foobar"
Returns a repeated string given a multiplier.
repeat("1", 3)
// result => "111"
Reverse the input String
reverse("foo")
// result => "oof"
Returns a new string of a given length such that the ending of the string is padded.
rightPad("1", "0", 5)
// result => "10000"
Remove all spaces on right.
rightTrim("strman ")
// result => "strman"
Truncate the string securely, not cutting a word in half. It always returns the last full word.
safeTruncate("foo bar", 4, ".")
// result => "foo."
safeTruncate("A Javascript string manipulation library.", 16, "...")
// result => "A Javascript..."
Truncate the unsecured form string, cutting the independent string of required position.
truncate("A Javascript string manipulation library.", 14, "...")
// result => "A Javascrip..."
Converts all HTML entities to applicable characters.
htmlDecode("Ш")
// result => Ш
Convert all applicable characters to HTML entities.
htmlEncode("Ш")
// result => "Ш"
It returns a string with its characters in random order.
shuffle("shekhar")
Convert a String to a slug
slugify("foo bar")
// result => "foo-bar"
Remove all non valid characters. Example: change á => a or ẽ => e.
transliterate("fóõ bár")
// result => "foo bar"
Removes trailing characters from string.
trimEnd(" abc ")
// result => Optional(" abc")
trimEnd("")
// result Optional.empty()
Surrounds a 'value' with the given 'prefix' and 'suffix'.
surround("div", "<", ">"
// result => "<div>s"
tail("foobar")
// result => "oobar"
Transform to camelCase
toCamelCase("CamelCase")
// result => "camelCase"
toCamelCase("camel-case")
// result => "camelCase"
Transform to StudlyCaps.
toStudlyCase("hello world")
// result => "HelloWorld"
Decamelize String
toDecamelize("helloWorld",null)
// result => "hello world"
Transform to kebab-case.
toKebabCase("hello World")
// result => "hello-world"
Transform to snake_case.
toSnakeCase("hello world")
// result => "hello_world"
Converts the first character of string to upper case.
upperFirst("fred")
// result => "Fred"
Splits a String to words
words("This is a string, with words!")
// result => ["This", "is", "a", "string", "with", "words"]
Verifies whether String is enclosed by encloser
isEnclosedBetween("{{shekhar}}", "{{", "}}")
// result => true
Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
join(new String[]{"hello","world","123"}, ";")
// result => "hello;world;123")
isEmpty returns true if a string is blank or null and false otherwise.
isBlank("")
// result => true)
isBlank(null)
// result => true)
isBlank("test")
// result => false)
underscored
function converts a camel or dashed string into an underscored one
underscored("MozTransform")
// result => "moz_transform")
zip
function aggregates the contents of n strings into a single list of tuples.
zip("abc", "def")
// result => ["ad", "be", "cf"]
zip("abc", "d")
// result => ["ad"]
lines
split input to lines
lines("Hello\r\nWorld")
// result => ["Hello", "World"]
dasherize
converts a underscored or camelized string into an dasherized one.
dasherize("the_dasherize_string_method")
// result => "the-dasherize-string-method"
humanize
converts an underscored, camelized, or dasherized string into a humanized one.
humanize("the_humanize_method")
// result => "The humanize method"
swapCase
returns a copy of the string in which all the case-based characters have had their case swapped.
swapCase("AaBbCcDdEe")
// result => "aAbBcCdDeE"
chop
chops the input string into parts of length step
chop("whitespace", 3);
// result => ["whi", "tes", "pac", "e"]