Giter VIP home page Giter VIP logo

floating-floaties / string-utility Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 32 KB

Provides simple substring implementation (with the RangeBound trait), keep string before or after a specified string, etc.

Home Page: https://crates.io/crates/string-utility

License: GNU General Public License v3.0

Rust 100.00%
rust strings substring keep-after keep-before range range-bound trait substring-trait

string-utility's Introduction

Simple Substring Trait

Install

Add the following line to your Cargo.toml file (under [dependencies]):

string-utility = "0.2"

Usage

Import all Traits

use string_utility::prelude::*;

Substring

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);

  // get last char
  let result = some_text.substring_len(1); // same as (len - 1)..usize::MAX
  let expected = "!";
  assert_eq!(result, expected);
}

Keep

All functions: trait def

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);
}

string-utility's People

Contributors

dustindiazlopez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.