#substring #range #range-bound #keep-before #keep-after

string-utility

提供简单的子字符串实现(具有 RangeBound 特性),保留指定字符串之前或之后的字符串等。

2 个不稳定版本

0.2.0 2022 年 10 月 12 日
0.1.0 2022 年 10 月 9 日

#1494Rust 模式


inflection-rs 中使用

GPL-3.0-only

18KB
426

简单子字符串特质

安装

将以下行添加到您的 Cargo.toml 文件(在 [dependencies] 下)

string-utility = "0.2"

用法

导入所有特质

use string_utility::prelude::*;

子字符串

use string_utility::prelude::*;

fn main() {
    let some_text = "42Hello, world!".to_string();

    // cut first two chars
    let result = some_text.substring(2..);
    let expected = "Hello, world!";
    assert_eq!(result, expected);

    // extract world string
    let result = some_text.substring(9..=13);
    let expected = "world";
    assert_eq!(result, expected);

    // keep everything up to (including) idx 13
    let result = some_text.substring(..=13);
    let expected = "42Hello, world";
    assert_eq!(result, expected);

    // keep everything up to (excluding) idx 13
    let result = some_text.substring(..13);
    let expected = "42Hello, worl";
    assert_eq!(result, expected);

    // keep everything
    let result = some_text.substring(..);
    let expected = "42Hello, world!";
    assert_eq!(result, expected);

    // get last char
    let result = some_text.substring(some_text.len()-1..);
    let expected = "!";
    assert_eq!(result, expected);
}

保留

所有函数: 特质定义

use string_utility::prelude::*;

fn main() {
  let some_text = "some start value, some not-so-start value".to_string();
  
  let result = some_text
          .keep("start".to_string())  // keep(pattern)
          // from the
          .end_of_string()
          // whether to keep the pattern "start"
          .excluding_pattern() 
          // keep everything before the pattern
          .before_pattern()
          // exec
          .to_string();
    let expected = "some start value, some not-so-";
    assert_eq!(result, expected);
}

无运行时依赖