1 Star 0 Fork 2

天生骄傲 / underscore.string.java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

underscore.string.java Build Status CircleCI

A library for Java String manipulation

Latest Release

  • 0.2.0

More details in Release Notes

An introduction blog here

Prerequisites

  • java >= 1.6
  • guava 18.0

Installation

gradle

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.lambeta:underscore.string.java:0.2.0'
}

maven

<dependency>
    <groupId>com.lambeta</groupId>
    <artifactId>underscore.string.java</artifactId>
    <version>0.2.0</version>
</dependency>

API

capitalize

Converts first letter of the string to uppercase.

import static com.lambeta.UnderscoreString.capitalize;

capitalize(" hello ");
// -> "Hello"

decapitalize

Converts first letter of the string to lowercase.

import static com.lambeta.UnderscoreString.decapitalize;

decapitalize(" Hello ");
// -> "hello"
decapitalize("HELLO");
// -> "hELLO"

slugify

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"

count

Returns number of occurrences of substring in string.

import static com.lambeta.UnderscoreString.count;

count("Hello world", "l");
// -> 3

trim

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"

ltrim

Left trim. Similar to trim, but only for left side.

import static com.lambeta.UnderscoreString.ltrim;

ltrim(" foo");
// -> "foo"
ltrim("foof", "f");
// -> "oof"

rtrim

Right trim. Similar to trim, but only for right side.

import static com.lambeta.UnderscoreString.rtrim;

rtrim("foo ");
// -> "foo"
rtrim("foof", "f");
// -> "foo"

repeat

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"

join

Joins strings together with given separator.

import static com.lambeta.UnderscoreString.join;

join("", "foo", "bar");
// -> "foobar"
join("", null, "bar")
// -> "bar"

reverse

Return reversed string.

import static com.lambeta.UnderscoreString.reverse;

reverse("foo");
// -> "oof"

clean

Trim and replace multiple spaces with a single space.

import static com.lambeta.UnderscoreString.clean;

clean(" foo    bar   ");
// -> "foo bar"

chop

Chop given string to pieces.

import static com.lambeta.UnderscoreString.chop;

chop("whitespace", 2);
// -> ["wh", "it", "es", "pa", "ce"]

splice

Like an array splice. splice(start, deleteCount, item).

import static com.lambeta.UnderscoreString.splice;

splice("whitespace", 5, 5, "shift");
// -> "whiteshift"

pred

Returns the predecessor to string.

import static com.lambeta.UnderscoreString.pred;

pred('2');
// -> '1'

succ

Returns the successor to string.

import static com.lambeta.UnderscoreString.succ;

succ('a');
// -> 'b'

titleize

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"

camelize

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"

dasherize

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"

underscored

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"

classify

Converts string to camelized class name. First letter is always upper case.

import static com.lambeta.UnderscoreString.classify;

classify("some_class_name");
// -> "SomeClassName"

humanize

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

Surround a string with another string.

import static com.lambeta.UnderscoreString.surround;

surround("foo", "|");
// -> "|foo|"

quote

Quotes a string. quoteChar defaults to ".

import static com.lambeta.UnderscoreString.quote;

quote("foo");
// -> "\"foo\""

unquote

nquotes a string. quoteChar defaults to ".

import static com.lambeta.UnderscoreString.unquote;

unquote("\"foo\"");
// -> "foo"
unquote("'foo'", '\'');
// -> "foo"

numberFormat

Formats the numbers.

import static com.lambeta.UnderscoreString.numberFormat;

numberFormat(9000);
// -> "9,000"

strRight

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"

strRightBack

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"

strLeft

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"

strLeftBack

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"

toSentence

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

truncate given string with length.

import static com.lambeta.UnderscoreString.truncate;

truncate("Hello World", 5, "...");
// -> "Hello..."

lpad

left-pad a string.

import static com.lambeta.UnderscoreString.lpad;

lpad("Hello", 8);
// -> "   Hello"

rpad

right-pad a string.

import static com.lambeta.UnderscoreString.rpad;

rpad("Hello", 8);
// -> ("Hello   ")

lrpad

left/right-pad a string.

import static com.lambeta.Underscorestring.lrpad;

lrpad("1", 8);
// -> "    1   "

words

Split string by delimiter.

import static com.lambeta.Underscorestring.words;

words("I_love_you!");
// -> [ "I", "love", "you!" ]

prune

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..."

isBlank

Determine whether given string is blank or not.

import static com.lambeta.Underscorestring.isBlank;

isBlank("")
// -> true
isBlank(null);
// -> true
isBlank("\n");
// -> true

replaceAll

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");
// -> ""

swapCase

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"

naturalCmp

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

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"

commonPrefix

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"

commonSuffix

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"

chopPrefix

Remove prefix from the start of s. Otherwise return s.

import static com.lambeta.Underscorestring.chopPrefix;

chopPrefix("foo", "FOO")
// -> "foo"
chopPrefix("foo", "FOO", true);
// -> ""

chopSuffix

Remove suffix from the end of s. Otherwise return s.

import static com.lambeta.Underscorestring.chopSuffix;

chopSuffix("foo", "FOO");
// -> "foo"
chopSuffix("foo", "FOO", true);
// -> ""

screamingUnderscored

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"

stripAccents

Strip all accents (diacritical marks) from s.

import static com.lambeta.Underscorestring.stripAccents;

stripAccents("Et ça sera sa moitié");
// -> "Et ca sera sa moitie"

pascalize

Upper the case first char in s and use capitalization to separate words.

import static com.lambeta.Underscorestring.pascalize;

pascalize("PascalCase");
// -> "PascalCase"

translate

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"

mixedCase

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")

collapseWhitespaces

Convert all adjacent whitespace in s to a single space.

import static com.lambeta.Underscorestring.collapseWhitespaces;

collapseWhitespaces("foo    bar    baz");
// -> "foo bar baz"

ascii

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()

chomp

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"

startsWith

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

endsWith

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

levenshtein

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

hamming

Get the hamming distance between s1 and s2. refer to hamming distance.

import static com.lambeta.Underscorestring.hamming;

hamming("karolin", "kerstin");
// -> 3

longestCommonSubstring

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"} 
The MIT License (MIT) Copyright (c) 2015 Yan Qian Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

String manipulation operations in java 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/bing32/underscore.string.java.git
git@gitee.com:bing32/underscore.string.java.git
bing32
underscore.string.java
underscore.string.java
master

搜索帮助