0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

kotlin replace 文字 置き換え 置換

Last updated at Posted at 2022-05-23

Kotlin replace

役割: 文字置き換え

使える型

CharSequence

使うシーン

文字列の特定の文字を他の特定の文字に置き換える
文字列の特定の文字や正規表現に合致する文字列を削除する

使い方

replaceExample
replace("置換前の文字列","置換後の文字列")
replace({正規表現},"置換後の文字列")
※置換前,置換後の文字列は、Char or String

特定の文字列を他の特定の文字列で置換する例

replace
  fun replace(){
    val mojiString = "qenqinaqleaqqleqen"
    val mojiStringReplaced = mojiString.replace("q","p")
    println(mojiStringReplaced)
  }
result
penpinapleapplepen

正規表現に当てはまる文字列を特定の文字列に置換する例

replace
  fun replace(){
    val mojiString = "pen10pi1nap2le5app9lepe0n"
    val mojiStringReplaced = mojiString.replace(Regex("[0-9]"),"")
    println(mojiStringReplaced)
  }
  }
result
penpinapleapplepen

今回は、空白に置き換えたので、数字を削除することができました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?