Underscore.string.java 是什么?
underscore.string.java 是一個(gè)Java中方便操作字符串的類庫(kù),提供了眾多幫助方法。
起源
寫過(guò) Javascript 代碼的人,估計(jì)沒有幾個(gè)不知道 underscore 這個(gè)類庫(kù)的,因?yàn)樗茫ㄓ校┯昧耍M管現(xiàn)如今由于實(shí)現(xiàn)上不夠優(yōu)雅的緣故,已經(jīng)被lodash所取代。而我想介紹的是 github 上 star 3000+的 underscore.string,它原本是 underscore 的擴(kuò)展,不過(guò)現(xiàn)在已經(jīng)演變成獨(dú)立的庫(kù)。顧名思義,它的作用就是彌補(bǔ) Javascript 本身對(duì)于字符串操作支持的匱乏。
舉個(gè)例子:
var slugify = require("underscore.string/slugify");
slugify("Hello world!");
-> hello-world
slugify是一種規(guī)整字符串的操作,常用于把url中的非法字符規(guī)整成 word-word 的模樣。比如,我的這篇 blog 是通過(guò) hexo 生成的 hexo new "Underscore.string.java",它會(huì)自動(dòng)幫我轉(zhuǎn)換成 2017-09-06-Underscore-string-java 這樣的親和url的格式。
想法
接觸這個(gè)類庫(kù)的時(shí)候,我正在使用 google guava,驚訝于這里頭的各類操作,包括 ImmutableList、CharMatcher、Strings 等等。一來(lái)準(zhǔn)備針對(duì) guava 練練手,二來(lái)確實(shí)想學(xué)習(xí)一下 underscore.string 的操作,所以就開始著手寫起了 underscore.string.java 這個(gè)項(xiàng)目。編寫的過(guò)程中確實(shí)學(xué)到了不少平常不太可能接觸到的設(shè)計(jì)方法。
項(xiàng)目介紹
這個(gè)類庫(kù)已經(jīng)正式發(fā)布在 Maven Central Repository. 最新版本是 0.2.0.
前置條件
- java >= 1.6
- guava 18.0
安裝
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>
支持的特性
- capitalize
- slugify
- count
- trim
- ltrim
- rtrim
- repeat
- decapitalize
- join
- reverse
- clean
- chop
- splice
- pred
- succ
- titleize
- camelize
- dasherize
- underscored
- classify
- humanize
- quote
- unquote
- surround
- numberFormat
- strRight
- strRightBack
- strLeft
- strLeftBack
- toSentence
- truncate
- lpad
- rpad
- lrpad
- words
- prune
- isBlank
- replaceAll
- swapCase
- naturalSort
- naturalCmp
- dedent
- commonPrefix
- commonSuffix
- chopPrefix
- chopSuffix
- screamingUnderscored
- stripAccents
- pascalize
- translate
- mixedCase
- collapseWhitespaces
- ascii
- chomp
- startsWith
- endsWith
- levenshtein
- hamming
- longestCommonSubstring
New Features in 0.2.1-SNAPSHOT
gradle
repositories {
maven {
url 'https://oss.sonatype.org/content/groups/public'
}
}
dependencies {
compile ("com.lambeta:underscore.string.java:0.2.1-SNAPSHOT")
}
maven
<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>
- replaceZeroWidthDelimiterWith