[][src]Function phpify::string::substr

pub fn substr<S>(string: S, start: isize, length: isize) -> Option<String> where
    S: AsRef<str>, 

Return part of a string.

Description

Returns the portion of string specified by the start and length parameters.

Parameters

start

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

If string is less than start characters long, None will be returned.

length

If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, NONE will be returned.

If length is given and is 0, an empty string will be returned.

Examples

Example #1 substr() examples

use phpify::string::substr;

assert_eq!(substr("abcdef", 1, std::isize::MAX).unwrap(), "bcdef");
assert_eq!(substr("abcdef", 1, 3).unwrap(), "bcd");
assert_eq!(substr("abcdef", 0, 4).unwrap(), "abcd");
assert_eq!(substr("abcdef", 0, 8).unwrap(), "abcdef");
assert_eq!(substr("abcdef", -1, 1).unwrap(), "f");

Example #2 using a negative start

use phpify::string::substr;

assert_eq!(substr("abcdef", -1, std::isize::MAX).unwrap(), "f");
assert_eq!(substr("abcdef", -2, std::isize::MAX).unwrap(), "ef");
assert_eq!(substr("abcdef", -3, 1).unwrap(), "d");

Example #3 using a negative length

use phpify::string::substr;

assert_eq!(substr("abcdef", 0, -1).unwrap(), "abcde");
assert_eq!(substr("abcdef", 2, -1).unwrap(), "cde");
assert_eq!(substr("abcdef", 4, -4), None);
assert_eq!(substr("abcdef", -3, -1).unwrap(), "de");