본문 바로가기

코틀린/[Mapping] 매핑 작업14

[Kotlin][String] trim / trimStart / trimEnd 술어와 일치하는 선행 및 후행 문자가 제거된 이 문자 시퀀스의 하위 시퀀스를 반환합니다.public inline fun CharSequence.trim(predicate: (Char) -> Boolean): CharSequence { var startIndex = 0 var endIndex = length - 1 var startFound = false while (startIndex  술어와 일치하는 선행 문자가 제거된 문자열을 반환합니다.public inline fun CharSequence.trimStart(predicate: (Char) -> Boolean): CharSequence { for (index in this.indices) if (!predica.. 2024. 8. 30.
[Kotlin][Collection] toTypedArray 이 컬렉션의 모든 요소를 ​​포함하는 형식화된 배열을 반환합니다. 이 컬렉션의 크기와 동일한 크기를 갖는 런타임 유형 T 의 배열을 할당하고 이 컬렉션의 요소로 배열을 채웁니다.public actual inline fun Collection.toTypedArray(): Array { val thisCollection = this as java.util.Collection return thisCollection.toArray(arrayOfNulls(0)) as Array}val collection = listOf(1, 2, 3)val array = collection.toTypedArray()println(array.contentToString()) // [1, 2, 3] toTypedArra.. 2024. 8. 30.
[Kotlin][String] replaceRange 주어진 범위의 부분이 대체 문자 시퀀스로 대체되는 이 문자 시퀀스의 내용이 포함된 문자 시퀀스를 반환합니다.public fun CharSequence.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): CharSequence { if (endIndex  replaceRange - Kotlin Programming Language kotlinlang.org 2024. 8. 27.
[Kotlin][String] replace 모든 oldChar 가 newChar 로 대체된 새 문자열을 반환합니다.public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { run { var occurrenceIndex: Int = indexOf(oldValue, 0, ignoreCase) // FAST PATH: no match if (occurrenceIndex = length) break occurrenceIndex = indexOf(oldValue, occurrenceIndex + searchStep, ignoreCase) } whi.. 2024. 8. 27.
[Kotlin][Collection] zip 이 배열과 동일한 인덱스를 가진 다른 배열의 요소로 구성된 쌍 목록을 반환합니다. 반환된 목록에는 가장 짧은 컬렉션의 길이가 있습니다.public inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List { val first = iterator() val second = other.iterator() val list = ArrayList(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) while (first.hasNext() && second.hasNext()) { list.add(transform(first.n.. 2024. 8. 22.
[Kotlin][Collection] flatMap 원래 배열의 각 요소에 대해 호출된 변환 함수의 결과에서 생성된 모든 요소의 단일 목록을 반환합니다.public inline fun Iterable.flatMap(transform: (T) -> Iterable): List { return flatMapTo(ArrayList(), transform)}public inline fun > Iterable.flatMapTo(destination: C, transform: (T) -> Iterable): C { for (element in this) { val list = transform(element) destination.addAll(list) } return destination}val list = list.. 2024. 8. 6.
[Kotlin][Collection] map / mapIndexed 주어진 변환 함수를 원본 배열의 각 요소에 적용한 결과가 포함된 배열을 반환합니다.public inline fun Iterable.map(transform: (T) -> R): List { return mapTo(ArrayList(collectionSizeOrDefault(10)), transform)}public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { for (item in this) destination.add(transform(item)) return destination}val numbers = listOf(1, 2, 3)println(numbers.map { it * it }) /.. 2024. 8. 6.