搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

所在榜單