[Kotlin] substring 으로 문자열 자르기

Yuri Lee·2022년 8월 8일
0

Intro

kotlin 에서 substring 을 사용해서 문자열 자르는 방법에 대해 알아보도록 하자.

Substring()

fun String.substring(startIndex: Int): String
(Common source) (JVM source) (JS source) (Native source)
fun String.substring(startIndex: Int, endIndex: Int): String
(Common source) (JVM source) (JS source) (Native source)

Parameters

Substring 메서드의 파라미터는 아래와 같다. endIndex의 경우 exclusive (제외)된다.

  • startIndex : the start index (inclusive)
  • endIndex : the end index (exclusive)

Example

fun main() {
    println("substring 으로 문자열 자르기") // substring 으로 문자열 자르기
    val orginString = "kotlin"
    val newString= orginString.substring(0 until 5)
    println("newString : " + newString) // newString : kotli
}

참고

아래의 사이트에서 직접 코틀린을 실행해볼 수 있다.
https://play.kotlinlang.org/


https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/substring.html#kotlin.text$substring(kotlin.String,%20kotlin.ranges.IntRange)/range
https://kkh0977.tistory.com/641

profile
Step by step goes a long way ✨

0개의 댓글