1
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 chunked 文字 X文字ずつ 分割

Last updated at Posted at 2022-05-23

Kotlin chunked

役割: 文字列をX文字ずつ分割してリストにする

使える型

CharSequence

使うシーン

文字列型を特定の文字数で分割するとき

使い方

chunkedExample
chunked(X)
X文字ずつ分割する
XInt

1文字ずつ分割する例

chunkedExample
  fun chunked(){
    val mojiString = "abcdaebfghaibjklamnopbqrastu"
    val mojiChundkedList = mojiString.chunked(1)
    println(mojiChundkedList)
  }
result
[a, b, c, d, a, e, b, f, g, h, a, i, b, j, k, l, a, m, n, o, p, b, q, r, a, s, t, u]

2文字ずつ分割する例

chunkedExample
  fun chunked(){
    val mojiString = "ab cdaeb fgha ibjkla mno pbqra stu"
    val mojiChundkedList = mojiString.chunked(2)
    println(mojiChundkedList)
  }
result
[ab,  c, da, eb,  f, gh, a , ib, jk, la,  m, no,  p, bq, ra,  s, tu]

お、、、何故2文字ずつ分割したのに1文字の箇所があるの?
そう、今回の文字列は、半角スペースも含まれていたから。

半角スペースを削除するには、replaceメソッドを使えばいけそうですね!!!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?