繁体   English   中英

如何更改 rust 中字符串中特定索引处的字符?

[英]How do I change characters at a specific index within a string in rust?

我正在尝试更改字符串中特定索引处的单个字符,但我不知道如何在 rust 中更改。 例如,如何将“hello world”中的第 4 个字符更改为“x”,这样它就变成了“helxo world”?

最简单的方法是像这样使用replace_range()方法:

let mut hello = String::from("hello world");
hello.replace_range(3..4,"x");
println!("hello: {}", hello);

Output: hello: helxo world游乐场

请注意,如果要替换的范围不在 UTF-8 代码点边界上开始和结束,则会出现恐慌。 例如这会恐慌:

let mut hello2 = String::from("hell😀 world");
hello2.replace_range(4..5,"x"); // panics because 😀 needs more than one byte in UTF-8

如果要替换第 n 个 UTF-8 代码点,则必须执行以下操作:

pub fn main() {
    let mut hello = String::from("hell😀 world");
    hello.replace_range(
        hello
            .char_indices()
            .nth(4)
            .map(|(pos, ch)| (pos..pos + ch.len_utf8()))
            .unwrap(),
        "x",
    );
    println!("hello: {}", hello);
}

游乐场

在 Rust 中表示字符串的标准方法是编码为 UTF-8 字符串的连续字节范围。 UTF-8 代码点的长度可以是 1 到 4 个字节,因此通常不能简单地将一个 UTF-8 代码点替换为另一个代码点,因为长度可能会改变。 您也不能进行简单的指针运算来索引 Rust String到第 n 个字符,因为代码点编码的长度可以是 1 到 4 个字节。

因此,一种安全但缓慢的方法是这样的,遍历源字符串的字符,替换您想要的字符,然后创建一个新字符串:

fn replace_nth_char(s: &str, idx: usize, newchar: char) -> String {
    s.chars().enumerate().map(|(i,c)| if i == idx { newchar } else { c }).collect()
}

但是如果我们手动确保旧字符和新字符是单字节 ascii,我们可以在 O(1) 中做到这一点。

fn replace_nth_char_safe(s: &str, idx: usize, newchar: char) -> String {
    s.chars().enumerate().map(|(i,c)| if i == idx { newchar } else { c }).collect()
}

fn replace_nth_char_ascii(s: &mut str, idx: usize, newchar: char) {
    let s_bytes: &mut [u8] = unsafe { s.as_bytes_mut() };
    assert!(idx < s_bytes.len());
    assert!(s_bytes[idx].is_ascii());
    assert!(newchar.is_ascii());
    // we've made sure this is safe.
    s_bytes[idx] = newchar as u8;
}
fn main() {
    let s = replace_nth_char_safe("Hello, world!", 3, 'x');
    assert_eq!(s, "Helxo, world!");
    
    let mut s = String::from("Hello, world!");
    replace_nth_char_ascii(&mut s, 3, 'x');
    assert_eq!(s, "Helxo, world!");
}

请记住, replace_nth_char_ascii中的idx参数不是字符索引,而是字节索引。 如果字符串前面有任何多字节字符,则字节索引和字符索引将不对应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM