#string #substring #character #index #replace #indices #extract

substring-replace

此crate提供了一种易于开发者使用的字符串处理方法,用于通过字符索引操纵字符串

3个版本

0.1.7 2024年8月7日
0.1.6 2024年8月6日
0.1.3 2024年7月31日

#230文本处理

Download history 407/week @ 2024-07-27 379/week @ 2024-08-03 18/week @ 2024-08-10

804 每月下载量

GPL-2.0-or-later WITH Bison-exception-2…

17KB
121

mirror crates.io docs.rs

substring-replace: 提取、插入和替换子字符串

此crate添加了易于开发者使用的子字符串方法,可以方便地在Rust中使用字符索引操作字符串切片,其与JavaScript、Java或C#中的substring()方法、C++和PHP中的substr()方法类似,兼容多字节字符。

此crate的核心substring方法与更简单的substring crate具有相同的签名和功能,但添加了许多辅助方法,例如substring_replace,避免了使用不安全代码块,并在起始或结束索引超出范围时优雅地失败。然而,这两个crate不应在同一项目中使用。如果您只需要核心的substring方法并且已经使用了其他广泛支持的crate,请不要安装此crate。另一方面,如果您需要此crate提供的某些额外功能,请在安装此crate之前先卸载其他crate,并将use substring::*;替换为use substring_replace::*;

常规Rust倾向于使用字节索引范围来操作字符串。然而,如果字节索引超出范围或位于字符边界之间,则会引发panic。字符索引更直观且与流行的Regex crate兼容。

substring

返回起始和结束字符索引之间的子字符串。这些索引与多字节字符的扩展拉丁字母、大多数非拉丁字母、许多特殊符号和表情符号的字节索引不同。

let sample_str = "/long/file/path";
let result = sample_str.substring(5,9);
// the result is "file"

substring_replace

此方法删除指定起始和结束索引之间的字符,并插入一个替换字符串

let new_string = "azdefgh".substring_replace("bc", 1, 2);
println!("{}", new_string);
// will print "abcdefgh"

substring_insert

此方法在指定的字符索引处插入一个字符串,与标准 String::insert 方法不同,因为它使用字符索引而不是字节索引,以便更好地处理多字节字符。它还可以直接与 &str 一起使用,但返回一个新的所有字符串。

let sample_str = "a/c";
let result = sample_str.substring_insert("/b", 1);
// result will be "a/b/c"

substring_start

这将返回字符串的起始位置(strstring)直到指定的结束字符索引。

let sample_str = "/long/file/path";
let result = sample_str.substring_start(5);
// the result is "/long"

substring_end

此方法返回从指定起始字符索引到字符串的结束(&strstring)。

let sample_str = "/long/file/path";
let result = sample_str.substring_end(5);
// the result is "/file/path"

substring_replace_start

此方法替换字符串的起始部分到指定的结束字符索引。

// remove the first 2 characters and prepend the string "xyz"
let new_string = "abcdefgh".substring_replace_start("xyz", 2);
println!("{}", new_string);
// will print "xyzcdefgh"

substring_replace_end

此方法替换从指定起始字符索引开始字符串的剩余部分。

// remove all characters after and index of 3 and append the string "xyz"
let new_string = "abcdefgh".substring_replace_end("xyz", 3);
println!("{}", new_string);
// will print "abcxyz"

substring_remove

此方法返回从起始和结束字符索引之间删除子字符串后的剩余部分。它与 substring(start, end) 相反。

let sample_str = "abcdefghij";
let result = sample_str.substring_remove(3, 6);
// result will be "abcfghij"

substring_offset

此方法从起始索引提取一个子字符串到右侧或左侧的 n 个字符。第二个参数中的负长度将在参考索引处结束。

let sample_str = "indian-elephant";
let result = sample_str.substring_offset(7, 3);
// result will be "ele"

substring_pull

此方法返回从起始索引删除子字符串后的剩余部分(n 个字符到右侧或左侧)。它与 substring_offset(position, length) 相反。与 substring_offset 一样,第二个参数中的负长度将在参考索引处结束。

let sample_str = "indian-elephant";
let result = sample_str.substring_offset(7, 3);
// result will be "ele"
let result = sample_str.substring_offset(6, -3);
// result will be "ian"

to_start_byte_index 和 to_end_byte_index

这些方法将起始字符索引转换为起始字节索引或将结束字符索引转换为结束字节索引。它们主要用于内部构建字符串切片。它们之间的区别仅在于它们的默认值。对于 to_start_byte_index,默认值为 0,而对于 to_end_byte_index,它是最右边的索引。

let byte_index = "नमस्ते".to_start_byte_index(2);
// yields byte index of at the start of third multibyte character (character index 2). It should be 6

char_len

这返回以单个 Unicode 符号表示的字符长度,而不是与 str::len() 一起的字节长度。这是 &str::char_indices().count() 的简称。

let emoji = "😎";
println!("Emoji length: {}, emoji byte length: {}", emoji.char_len(), emoji.len() );
// prints: Emoji length: 1, emoji byte length: 4

char_find

此方法查找普通字符串模式的第一个字符索引。像标准的 find 方法一样,它返回一个可选的无符号整数(usize)。要从右向左搜索,但仍然返回匹配序列中第一个字符的索引,您可以使用 char_rfind

let greek_words = "μήλα και πορτοκάλια";
let search_word = "και";
let character_index = greek_words.char_find(search_word);
let byte_index = greek_words.find(search_word);
println!("The word {search_word} starts at a character index of {character_index} and a byte index of {byte_index}");
// The word $search_word starts at a character index of 5 and a byte index of 9

注意:这是一个 alpha 版本,但该软件包功能完善,补充了 string-patternssimple-string-patterns

版本历史

1.3: 添加了新方法 .substring_remove(start: usize, end: usize).substring_pull(position: usize, length: i32)

1.5: 添加了新的方法 .char_find(pat: &str).char_rfind(pat: &str)

无运行时依赖