Rust 文字列から後方にある数値を取り除く

  • 作成日 2022.10.06
  • 更新日 2022.12.02
  • Rust
Rust 文字列から後方にある数値を取り除く

Rustで、文字列から後方にある数値を取り除くサンプルコードを記述してます。Rustのバージョンは1.62.1を使用してます。

環境

  • OS windows11 home
  • rustc 1.62.1

文字列から後方にある数値を取り除く

文字列から後方にある数値を取り除くには「trim_end_matches(char::is_numeric)」を使用します。

"文字列".trim_end_matches(char::is_numeric)

実際に使用してみます。

fn main() {

    let mut str: String = "123abcde123".to_string();

    println!( "{}", str.trim_end_matches(char::is_numeric) ); // 123abcde

    str = "123abc123de123".to_string();

    println!( "{}", str.trim_end_matches(char::is_numeric) ); // 123abc123de

}

実行結果を見ると、取り除かれていることが確認できます。