A light-weight Java library that breaks strings into user-perceived characters a.k.a. Grapheme Clusters for common cases.
This library is a light-weight implementation with limited language support, based on java.text.BreakIterator
with some patches added, by a subset of rules in UAX #29: Unicode Text Segmentation. Checkout ICU4J if you need all rules implemented.
- Some rules of Hangul are not implemented, e.g. L + LVT.
- Some Spacing Mark Characters (e.g. Tamil, Myanmar) are not recognized.
GraphemeSplitter().split(
"\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67\uDB40\uDC7F\uD83E\uDD26\uD83C\uDFFB\u200D\u2642\uFE0F\uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66"
)
Result:
[๐ด๓ ง๓ ข๓ ฅ๓ ฎ๓ ง๓ ฟ, ๐คฆ๐ปโโ๏ธ, ๐จโ๐ฉโ๐งโ๐ฆ]
GraphemeSplitter().findBreaks(
"\uD83C\uDDF0\uD83C\uDDF7\u1112\u1161\u11AB"
)
Result:
[4, 7]
repositories {
maven("https://jitpack.io")
}
dependencies {
implementation("com.github.hiking93:grapheme-splitter-lite:$latestVersion")
}
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation "com.github.hiking93:grapheme-splitter-lite:$latestVersion"
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.hiking93</groupId>
<artifactId>grapheme-splitter-lite</artifactId>
<version>$tag</version>
</dependency>
Copyright 2021 Hsingchien Cheng
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.