搜索插件、代码仓与 APP 分类

GitHub 代码仓Chinmay-KB/stringr

stringr

适用于 Dart/Flutter 的字符串操作库,功能强大

stringr 项目封面
星标
6
复刻
3
最近推送(UTC)
2026年8月3日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

stringr

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.

Features

  • Case Conversion - camelCase, snake_case, kebab-case, Title Case, PascalCase
  • Unicode Support - Full support for non-Latin scripts, diacritics, and grapheme clusters
  • Smart Word Detection - Uses regex patterns instead of simple whitespace splitting
  • Null Safety - Fully null-safe and compatible with Dart 3.0+
  • Zero Dependencies - Only depends on the characters package for grapheme support

Installation

Add to your pubspec.yaml:

dependencies:
  stringr: ^1.2.0

Quick Start

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]
}

Unicode & Non-Latin Support

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)

API Overview

Case Manipulation
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'
String Chopping
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'
Counting
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
Manipulation
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'
Escaping
Method Description
escapeHtml() Escape HTML entities
unescapeHtml() Unescape HTML entities
escapeRegExp() Escape regex special chars

Why stringr?

Most string libraries fail with complex Unicode. stringr handles:

  • Grapheme clusters: 'café' is 4 characters, not 5
  • Surrogate pairs: Emoji and special symbols work correctly
  • Non-Latin scripts: Cyrillic, Greek, accented Latin, and more
  • Smart word detection: 'XMLHttpRequest' → ['XML', 'Http', 'Request']

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

Project Health

License

MIT License - see LICENSE for details.

Chinmay Kabi GitHub avatar
GitHub 作者

Chinmay Kabi ↗

SDE @blendto | Flutter Developer | Master Tinkerer

@blendtoBengaluru
语言Dart

此仓库发布的插件

使用的依赖

依赖清单 2 项

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗