slice_bytes
fn slice_bytes(string s, int start, int end) -> Result<string, IoError> Import
Source: use string::{slice_bytes}; compiler/src/builtin_docs/string.hy Returns the part of s between byte offsets start (inclusive) and end (exclusive).
Offsets are clamped to 0..=s.len(), and end to at least start, so
out-of-range offsets give a shorter (possibly empty) string rather than
an error. Returns Err(IoError::InvalidInput) when an offset falls
inside a multi-byte UTF-8 character.
Parameters#
| Name | Type |
|---|---|
s | string |
start | int |
end | int |
Returns#
Result<string, IoError>
Examples#
use io::{IoError};
use string::{slice_bytes};
use tests::assert;
let mid = match slice_bytes("hello", 1, 4) {
Result::Ok(s) => s,
Result::Err(_) => "",
};
assert!(mid == "ell");
let clamped = match slice_bytes("hello", 3, 100) {
Result::Ok(s) => s,
Result::Err(_) => "",
};
assert!(clamped == "lo");
let split_char = match slice_bytes("é", 0, 1) {
Result::Err(IoError::InvalidInput) => true,
default => false,
};
assert!(split_char);
See also#
byte_at: Returns the byte at byte offsetiofs, or-1wheniis out of range.find_from: Returns the byte offset of the firstneedleinhayat or afterstart, or-1.format: Builds a string from the literalfmtand the arguments that follow it.from_bytes: Decodesbytesas UTF-8 text.match_at: Returns whetherneedleoccurs insstarting exactly at byte offsetat.rfind: Returns the byte offset of the lastneedleinhay, or-1.to_bytes: Encodessas UTF-8 into a newVec<byte>.