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
}
実行結果を見ると、取り除かれていることが確認できます。
-
前の記事
javascript videoタグの一時停止のイベントを取得する 2022.10.06
-
次の記事
Fork ブランチの名前を変更する 2022.10.06
コメントを書く