v1.2.2
stringr
适用于 Dart 的全面字符串操作插件。可同时处理拉丁文、非拉丁文和字符簇!功能灵感来自 VocaJs
20喜欢 2.4万近 30 天下载160Pub 分数
AndroidiOSWebmacOSWindowsLinux
适用于 Dart/Flutter 的字符串操作库,功能强大
^1.3.0^1.26.3以下为英文项目原文快照,最新内容请访问 GitHub。
pub package codecov License: MIT
A comprehensive string manipulation library for Dart and Flutter. Inspired by VocaJS, stringr provides powerful string operations that work seamlessly with Latin, non-Latin, and Unicode strings.
characters package for grapheme supportAdd to your pubspec.yaml:
dependencies:
stringr: ^1.2.0
import 'package:stringr/stringr.dart';
void main() {
// Case conversions
print('hello world'.toCamelCase); // helloWorld
print('hello world'.toSnakeCase); // hello_world
print('hello world'.toKebabCase); // hello-world
print('hello world'.toPascalCase); // HelloWorld
// Smart word extraction
print('XMLHttpRequest'.camelCase()); // xmlHttpRequest
print('/path/to/file'.camelCase()); // pathToFile
print('2infinity&beyond'.words()); // [2, infinity, beyond]
}
stringr shines with complex Unicode strings:
// Non-Latin scripts
print('полет птицы'.kebabCase()); // полет-птицы
print('La variété française'.prune(12)); // La variété
// Diacritics and latinization
print('café'.latinize()); // cafe
print('anglikonų šiurkščios'.latinize()); // anglikonu siurkscios
// Grapheme-aware operations
print('café'.countGrapheme()); // 4 (not 5!)
print('👨👩👧👦'.reverse()); // 👨👩👧👦 (family emoji stays intact)
| Method | Description | Example |
|---|---|---|
camelCase() |
Convert to camelCase | 'foo bar' → 'fooBar' |
snakeCase() |
Convert to snake_case | 'foo bar' → 'foo_bar' |
kebabCase() |
Convert to kebab-case | 'foo bar' → 'foo-bar' |
titleCase() |
Capitalize each word | 'foo bar' → 'Foo Bar' |
capitalize() |
Capitalize first char | 'foo' → 'Foo' |
swapCase() |
Toggle case | 'FoO' → 'fOo' |
| Method | Description | Example |
|---|---|---|
truncate(n) |
Truncate to n chars | 'hello world' → 'hello...' |
prune(n) |
Truncate without breaking words | 'hello world' → 'hello' |
first(n) |
Get first n chars | 'hello' → 'hel' |
last(n) |
Get last n chars | 'hello' → 'llo' |
slice(start, end) |
Extract substring | 'hello' → 'ell' |
| Method | Description | Example |
|---|---|---|
countGrapheme() |
Count visible characters | 'café' → 4 |
countOccurrences(s) |
Count substring occurrences | 'banana'.countOccurrences('a') → 3 |
countWords() |
Count words | 'hello world' → 2 |
countWhere(fn) |
Count matching chars | Custom predicate |
| Method | Description | Example |
|---|---|---|
reverse() |
Reverse (grapheme-aware) | 'hello' → 'olleh' |
slugify() |
URL-friendly slug | 'Hello World!' → 'hello-world' |
latinize() |
Remove diacritics | 'café' → 'cafe' |
insert(s, i) |
Insert at position | 'hllo'.insert('e', 1) → 'hello' |
| Method | Description |
|---|---|
escapeHtml() |
Escape HTML entities |
unescapeHtml() |
Unescape HTML entities |
escapeRegExp() |
Escape regex special chars |
Most string libraries fail with complex Unicode. stringr handles:
'café' is 4 characters, not 5'XMLHttpRequest' → ['XML', 'Http', 'Request']Contributions are welcome! Please read our Contributing Guide for details.
MIT License - see LICENSE for details.