Add starts_with and ends_with for strings.

This commit is contained in:
Stephen Chung 2022-03-23 14:28:45 +08:00
parent 99118fe2c3
commit cad4c7a942
2 changed files with 30 additions and 0 deletions

View File

@ -31,6 +31,7 @@ Enhancements
* Separation of constants in function calls is removed as its performance benefit is dubious.
* A function `sleep` is added to block the current thread by a specified number of seconds.
* `Scope::set_alias` is added to export a variable under a particular alias name.
* `starts_with` and `ends_with` are added for strings.
Version 1.5.0

View File

@ -458,6 +458,35 @@ mod string_functions {
*character = to_lower_char(*character)
}
/// Return `true` if the string starts with a specified string.
///
/// # Example
///
/// ```rhai
/// let text = "hello, world!";
///
/// print(text.starts_with("hello")); // prints true
///
/// print(text.starts_with("world")); // prints false
/// ```
pub fn starts_with(string: &str, match_string: &str) -> bool {
string.starts_with(match_string)
}
/// Return `true` if the string ends with a specified string.
///
/// # Example
///
/// ```rhai
/// let text = "hello, world!";
///
/// print(text.ends_with("world!")); // prints true
///
/// print(text.ends_with("hello")); // prints false
/// ```
pub fn ends_with(string: &str, match_string: &str) -> bool {
string.ends_with(match_string)
}
/// Find the specified `character` in the string, starting from the specified `start` position,
/// and return the first index where it is found.
/// If the `character` is not found, `-1` is returned.