LoginSignup
1
0

【Android】TextViewで動的に前後のスペースを消す方法【Kotlin】

Posted at

はじめに

今回はTextViewに設定したテキストのスペースを動的に消す方法を記事に残します。
文字にしてみると語彙力のなさの影響か わかりにくくなりますが、簡単にいうと、
" Hello World! "→ "Hello World!"というようにしたいということです。

trim()

trimメソッドを利用します。

    val text = " Hello World! "
    text.trim()

また、 前のスペースだけ覗きたい場合は

    val text = " Hello World! "
    text.trimStart()

同じように、後ろのスペースだけ覗きたい場合は

    val text = " Hello World! "
    text.trimEnd()

注意事項として trimメソッドで取り除けるのは半角スペースのみだそうです。

1
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
1
0