stringslice
A collection of methods to slice strings based on character indices rather than bytes.
This crate implements the StringSlice
trait for &str
,
containing the slice
, try_slice
, substring
, and try_substring
methods.
&str
and standard String
types#[no_std]
compatible by defaultAdd stringslice
to your Cargo.toml
file:
toml
[dependencies]
stringslice = "0.1"
The slice
method can be used to slice a &str
.
```rust use stringslice::StringSlice;
asserteq!("Ùníc😎de".slice(4..5), "😎"); asserteq!("世界こんにちは".slice(2..), "こんにちは"); ```
The substring
method is provided for convenience and accepts
separate parameters for the start and end of the slice.
```rust use stringslice::StringSlice;
assert_eq!("Γεια σου κόσμε".substring(9, 14), "κόσμε"); ```
There are also equivalent try_slice
and try_substring
methods
which return None
for invalid input.
```rust use stringslice::StringSlice;
asserteq!("string".tryslice(4..2), None); ```
Licensed under either of
at your option.