A library for Java String manipulation
- 0.2.0
More details in Release Notes
- java >= 1.7
- guava 18.0
repositories {
mavenCentral()
}
dependencies {
compile 'com.lambeta:underscore.string.java:0.2.0'
}
<dependency>
<groupId>com.lambeta</groupId>
<artifactId>underscore.string.java</artifactId>
<version>0.2.0</version>
</dependency>
Converts first letter of the string to uppercase.
import static com.lambeta.UnderscoreString.capitalize;
capitalize(" hello ");
// -> "Hello"
Converts first letter of the string to lowercase.
import static com.lambeta.UnderscoreString.decapitalize;
decapitalize(" Hello ");
// -> "hello"
decapitalize("HELLO");
// -> "hELLO"
Transform text into an ascii slug which can be used in safely in URLs. Replaces whitespaces, accentuated, and special characters with a dash. Limited set of non-ascii characters are transformed to similar versions in the ascii character set such as ä to a.
import static com.lambeta.UnderscoreString.slugify;
slugify(" hello World!");
// -> "hello-world"
slugify("Un éléphant àß l\'orée du bois");
// -> "un-elephant-ass-l-oree-du-bois"
Returns number of occurrences of substring in string.
import static com.lambeta.UnderscoreString.count;
count("Hello world", "l");
// -> 3
Trims defined characters from begining and ending of the string. Defaults to whitespace characters.
import static com.lambeta.UnderscoreString.trim;
trim(" foo ");
// -> "foo"
trim("foo", "f");
// -> "oo"
Left trim. Similar to trim, but only for left side.
import static com.lambeta.UnderscoreString.ltrim;
ltrim(" foo");
// -> "foo"
ltrim("foof", "f");
// -> "oof"
Right trim. Similar to trim, but only for right side.
import static com.lambeta.UnderscoreString.rtrim;
rtrim("foo ");
// -> "foo"
rtrim("foof", "f");
// -> "foo"
Repeats a string count times.
import static com.lambeta.UnderscoreString.repeat;
repeat("foo");
// -> ""
repeat("foo", 3);
// -> "foofoofoo"
repeat("foo", 3, 3);
// -> "foo3foo3foo"
repeat("foo", 3, new Person("ryan", "male"));
// -> "fooPerson{name=ryan, gender=male}fooPerson{name=ryan, gender=male}foo"
Joins strings together with given separator.
import static com.lambeta.UnderscoreString.join;
join("", "foo", "bar");
// -> "foobar"
join("", null, "bar")
// -> "bar"
Return reversed string.
import static com.lambeta.UnderscoreString.reverse;
reverse("foo");
// -> "oof"
Trim and replace multiple spaces with a single space.
import static com.lambeta.UnderscoreString.clean;
clean(" foo bar ");
// -> "foo bar"
Chop given string to pieces.
import static com.lambeta.UnderscoreString.chop;
chop("whitespace", 2);
// -> ["wh", "it", "es", "pa", "ce"]
Like an array splice. splice(start, deleteCount, item)
.
import static com.lambeta.UnderscoreString.splice;
splice("whitespace", 5, 5, "shift");
// -> "whiteshift"
Returns the predecessor to string.
import static com.lambeta.UnderscoreString.pred;
pred('2');
// -> '1'
Returns the successor to string.
import static com.lambeta.UnderscoreString.succ;
succ('a');
// -> 'b'
Capitalize each word of the given string like a title.
import static com.lambeta.UnderscoreString.titleize;
titleize("the titleize string method");
// -> "The Titleize String Method"
Converts underscored or dasherized string to a camelized one. Begins with a lower case letter unless it starts with an underscore, dash or an upper case letter.
import static com.lambeta.UnderscoreString.camelize;
camelize("the_camelize_string_method");
// -> "theCamelizeStringMethod"
camelize("_webkit _ transform ");
// -> "WebkitTransform"
Converts a underscored or camelized string into an dasherized one.
import static com.lambeta.UnderscoreString.dasherize;
dasherize("the_dasherize_string_method");
// -> "the-dasherize-string-method"
Converts a camelized or dasherized string into an underscored one.
import static com.lambeta.UnderscoreString.underscored;
underscored("the-underscored-string-method");
// -> "the_underscored_string_method"
Converts string to camelized class name. First letter is always upper case.
import static com.lambeta.UnderscoreString.classify;
classify("some_class_name");
// -> "SomeClassName"
Converts an underscored, camelized, or dasherized string into a humanized one.
import static com.lambeta.UnderscoreString.humanize;
humanize("the humanize string method");
// -> "The humanize string method"
humanize("the humanize_id string method");
// -> "The humanize id string method"
Surround a string with another string.
import static com.lambeta.UnderscoreString.surround;
surround("foo", "|");
// -> "|foo|"
Quotes a string. quoteChar defaults to ".
import static com.lambeta.UnderscoreString.quote;
quote("foo");
// -> "\"foo\""
nquotes a string. quoteChar defaults to ".
import static com.lambeta.UnderscoreString.unquote;
unquote("\"foo\"");
// -> "foo"
unquote("'foo'", '\'');
// -> "foo"
Formats the numbers.
import static com.lambeta.UnderscoreString.numberFormat;
numberFormat(9000);
// -> "9,000"
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
import static com.lambeta.UnderscoreString.strRight;
strRight("This_is_a_test_string", "_");
// -> "is_a_test_string"
Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
import static com.lambeta.UnderscoreString.strRightBack;
strRightBack("This_is_a_test_string", "_");
// -> "string"
Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
import static com.lambeta.UnderscoreString.strLeft;
strLeft("This_is_a_test_string", "_");
// -> "This"
Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
import static com.lambeta.UnderscoreString.strLeftBack;
strLeftBack("This_is_a_test_string", "_");
// -> "This_is_a_test"
Join an array into a human readable sentence.
import static com.lambeta.UnderscoreString.toSentence;
String[] words = new String[] {"Hello", "Welcome"};
toSentence(words);
// -> "Hello and Welcome"
truncate given string with length.
import static com.lambeta.UnderscoreString.truncate;
truncate("Hello World", 5, "...");
// -> "Hello..."
left-pad a string.
import static com.lambeta.UnderscoreString.lpad;
lpad("Hello", 8);
// -> " Hello"
right-pad a string.
import static com.lambeta.UnderscoreString.rpad;
rpad("Hello", 8);
// -> ("Hello ")
left/right-pad a string.
import static com.lambeta.Underscorestring.lrpad;
lrpad("1", 8);
// -> " 1 "
Split string by delimiter.
import static com.lambeta.Underscorestring.words;
words("I_love_you!");
// -> [ "I", "love", "you!" ]
Elegant version of truncate. Makes sure the pruned string does not exceed the original length. Avoid half-chopped words when truncating.
import static com.lambeta.Underscorestring.prune;
prune("Hello, cruel world", 15);
// -> "Hello, cruel..."
Determine whether given string is blank or not.
import static com.lambeta.Underscorestring.isBlank;
isBlank("")
// -> true
isBlank(null);
// -> true
isBlank("\n");
// -> true
Replace all find str in given string with replacement, if given string is null or empty, then returns empty string. The last argument true means ignore cases.
import static com.lambeta.Underscorestring.replaceAll;
replaceAll("aca", "a", "b");
// -> "bcb"
replaceAll("Aa", "a", "b", true);
// -> "bb"
replaceAll("", "a", "b");
// -> ""
Returns a copy of the string in which all the case-based characters have had their case swapped.
import static com.lambeta.Underscorestring.swapCase;
swapCase("Hello World");
// -> "hELLO wORLD"
swapCase("ß");
// -> "SS"
Naturally sort strings like humans would do. None numbers are compared by their ASCII values. Note: this means "a" > "A". Use .toLowerCase if this isn't to be desired.
import static com.lambeta.Underscorestring.naturalCmp;
naturalCmp("abc", "123");
// -> 1
naturalCmp("15a123", "15a122");
// -> 1
naturalCmp("r9", "r69");
// -> -1
Dedent unnecessary indentation.
import static com.lambeta.Underscorestring.dedent;
dedent(" Hello\n World");
// -> " Hello\nWorld"
dedent("\t\tHello\tWorld");
// -> "Hello\tWorld"
dedent("\t\tHello\n\t\tWorld");
// -> "Hello\nWorld"
Returns the longest common prefix of s and s1. given ignoreCase as true will return common suffix of s1.
import static com.lambeta.Underscorestring.commonPrefix;
commonPrefix("123456", "123o8yuidfg");
// -> "123"
commonPrefix("Hello", "helloo", true);
// -> "hello"
Returns the longest common suffix of s and s1. given ignoreCase as true will return common suffix of s1.
import static com.lambeta.Underscorestring.commonSuffix;
commonSuffix("456123", "1414123");
// -> "123"
commonSuffix("hello", "hellO", true);
// -> "hellO"
Remove prefix from the start of s. Otherwise return s.
import static com.lambeta.Underscorestring.chopPrefix;
chopPrefix("foo", "FOO")
// -> "foo"
chopPrefix("foo", "FOO", true);
// -> ""
Remove suffix from the end of s. Otherwise return s.
import static com.lambeta.Underscorestring.chopSuffix;
chopSuffix("foo", "FOO");
// -> "foo"
chopSuffix("foo", "FOO", true);
// -> ""
Upper case s and use underscores to separate words.
import static com.lambeta.Underscorestring.screamingUnderscored;
screamingUnderscored("The-Underscored_String_-Method");
// -> "THE_UNDERSCORED_STRING_METHOD"
screamingUnderscored("HTTPRequest");
// -> "HTTP_REQUEST"
screamingUnderscored("setID");
// -> "SET_ID"
Strip all accents (diacritical marks) from s.
import static com.lambeta.Underscorestring.stripAccents;
stripAccents("Et ça sera sa moitié");
// -> "Et ca sera sa moitie"
Upper the case first char in s and use capitalization to separate words.
import static com.lambeta.Underscorestring.pascalize;
pascalize("PascalCase");
// -> "PascalCase"
Translate all characters in s according to the mappings found in tmap (second arg).
Any characters found in the set delete-chars (third arg) will be pruned prior to consulting tmap.
Any characters mapping to null
in tmap will also be deleted.
import static com.lambeta.Underscorestring.translate;
translate("ababa", new HashMap<Character, Character>(){{put('a', 'b');}});
// -> "bbbbb"
translate("ababa", new HashMap<Character, Character>(){{put('a', 'b');}}, new HashSet<Character>(){{add('b');}});
// -> "bbb"
Return Optional<String> s
if s contains both upper and lower case letters.
import static com.lambeta.Underscorestring.mixedCase;
mixedCase("1AB");
// -> Optional.<String>absent();
mixedCase("FooBar");
// -> Optional.<String>of("FooBar")
Convert all adjacent whitespace in s to a single space.
import static com.lambeta.Underscorestring.collapseWhitespaces;
collapseWhitespaces("foo bar baz");
// -> "foo bar baz"
Return Optional<String> s
if s only contains ASCII characters.
import static com.lambeta.Underscorestring.ascii;
ascii("ascii");
// -> Optional<String>.of("ascii")
ascii("Et ça sera sa moitié");
// -> Optional<String>.absent()
Return a new string with \r\n
, \n
or \r
removed from the end.
import static com.lambeta.Underscorestring.chomp;
chomp("foo\n");
// -> "foo"
chomp("foo\n\r");
// -> "foo\n"
Return true if s starts with prefix. If the third argument is provided as true
, the string comparison is insensitive to case.
import static com.lambeta.Underscorestring.startsWith;
startsWith("foo", "foo");
// -> true
startsWith("foo", "foobar");
// -> false
startsWith("Foo", "foo", true);
// -> true
Return true if s ends with suffix. If the third argument is provided as true
, the string comparison is insensitive to case.
import static com.lambeta.Underscorestring.endsWith;
endsWith("foobar", "bar");
// -> true
endsWith("fooBar", "bar");
// -> false
endsWith("fooBar", "bar", true);
// -> true
Get the edit distance between s1 and s2.
import static com.lambeta.Underscorestring.levenshtein;
levenshtein("Godfather", "Godfather");
// -> 0
levenshtein("Godfather", "Gdfthr");
// -> 3
levenshtein("因為我是中國人所以我會說中文", "因為我是英國人所以我會說英文");
// -> 2
levenshtein("lol", null);
// -> 3
levenshtein(null, "lol");
// -> 3
Get the hamming distance between s1 and s2. refer to hamming distance.
import static com.lambeta.Underscorestring.hamming;
hamming("karolin", "kerstin");
// -> 3
Returns the set of the longest common substrings in s1 and s2.
This implementation uses dynamic programming, and not a generalized suffix tree, so the runtime is O(nm).
import static com.lambeta.Underscorestring.longestCommonSubstring;
longestCommonSubstring("fooquxbar", "foobar");
// -> {"foo", "bar"}
repositories {
maven {
url 'https://oss.sonatype.org/content/groups/public'
}
}
dependencies {
compile ("com.lambeta:underscore.string.java:0.2.1-SNAPSHOT")
}
<repositories>
<repository>
<id>my-repo</id>
<name>sonatype</name>
<url>https://oss.sonatype.org/content/groups/public</url>
</repository>
</repositories>
<dependency>
<groupId>com.lambeta</groupId>
<artifactId>underscore.string.java</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>
Replaces the zero width delimiter between two Uppercase characters or character and number and so on.
import static com.lambeta.Underscorestring.replaceZeroWidthDelimiterWith;
replaceZeroWidthDelimiterWith("GL11Version", " ");
// -> "GL 11 Version"
replaceZeroWidthDelimiterWith("SimpleXMLParser", " ");
// -> "Simple XML Parser"