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

插件详情v1.2.2

stringr

适用于 Dart 的全面字符串操作插件。可同时处理拉丁文、非拉丁文和字符簇!功能灵感来自 VocaJs

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
7.3万
喜欢数
20
Pub 分数
160

安装插件

flutter pub add stringr

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Stringr 是一个功能全面的 Dart 包,用于高级字符串操作,支持拉丁文、非拉丁文以及字形簇处理,精度极高。受 Voca.js 启发,它提供了一套丰富的方法,可用于跨多种脚本的文本转换和分析。非常适合需要强大字符串操作的国际化应用,确保对复杂 Unicode 字符进行准确处理。该包可无缝集成到所有主要平台上的 Flutter 应用中。

适合场景

  • 国际化文本处理
  • 文本归一化和清理
  • 字符串转换工具
  • 处理多语言内容
  • 数据验证和清理
  • 字形簇感知操作

主要特点

  • 对拉丁文和非拉丁文脚本的支持
  • 字形簇意识
  • 受 Voca.js 启发的 API 设计
  • 跨平台兼容性
  • Unicode 安全的字符串操作
  • 丰富的文本操作方法集

适合对象

  • 需要多语言支持的应用程序
  • 需要强大字符串工具的开发者
  • 涉及文本分析的项目
  • 面向全球受众的Flutter应用
  • 使用大量Unicode数据的团队

使用建议

  • 可能为简单的字符串任务增加开销
  • 需要了解 Unicode 和字素
  • 除了基本用法外,文档有限
  • 未针对高性能批量操作进行优化
  • 最适合中等至复杂的字符串需求

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

所在榜单