How remove specific char from String in Rust?

Hi, how can i remove these specific chars in this String ?
To optain "Bob esponja" ?

Bob esponja [2016] - 090

I can´t remove:
2016 - 090

Are you always trying to remove everything after the first '[', plus any preceding whitespace? If so, here's one way:

    if let Some(idx) = name.split('[').next().map(|s| str::trim_end().len()) {
        name.truncate(idx);
    }

Playground.

Or if you want a new string instead of modifying the one you have, that'd be

    let new_name = name
        .split('[')
        .next()
        .map(str::trim)
        .unwrap_or(&name)
        .to_string();
2 Likes

Thank you so much.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.