문자열 변환 함수
- toUpperCase() / uppercase()
- 문자열의 모든 문자를 대문자로 변환합니다.
val str = "hello" val upperStr = str.toUpperCase() // 또는 str.uppercase() println(upperStr) // "HELLO" - toLowerCase() / lowercase()
- 문자열의 모든 문자를 소문자로 변환합니다.
val str = "HELLO" val lowerStr = str.toLowerCase() // 또는 str.lowercase() println(lowerStr) // "hello" - capitalize()
- 문자열의 첫 글자를 대문자로 변환합니다.
val str = "hello" val capitalizedStr = str.capitalize() println(capitalizedStr) // "Hello" - decapitalize()
- 문자열의 첫 글자를 소문자로 변환합니다.
val str = "Hello" val decapitalizedStr = str.decapitalize() println(decapitalizedStr) // "hello"
문자열 조작 함수
- replace(oldValue: String, newValue: String)
- 문자열 내의 특정 문자열을 다른 문자열로 치환합니다.
val str = "hello world" val replacedStr = str.replace("world", "Kotlin") println(replacedStr) // "hello Kotlin" - replaceFirst(oldValue: String, newValue: String)
- 문자열 내에서 첫 번째로 매칭되는 특정 문자열을 다른 문자열로 치환합니다.
val str = "hello world world" val replacedFirstStr = str.replaceFirst("world", "Kotlin") println(replacedFirstStr) // "hello Kotlin world" - substring(startIndex: Int, endIndex: Int)
- 문자열의 특정 부분을 추출합니다.
val str = "hello world" val substr = str.substring(0, 5) println(substr) // "hello" - trim()
- 문자열의 양 끝 공백을 제거합니다.
val str = " hello world " val trimmedStr = str.trim() println(trimmedStr) // "hello world" - trimStart() / trimEnd()
- 문자열의 앞쪽 또는 뒤쪽 공백을 제거합니다.
val str = " hello world " val trimmedStartStr = str.trimStart() println(trimmedStartStr) // "hello world " val trimmedEndStr = str.trimEnd() println(trimmedEndStr) // " hello world" - split(delimiter: String)
- 문자열을 특정 구분자로 분리하여 리스트로 반환합니다.
val str = "hello world" val splitStr = str.split(" ") println(splitStr) // ["hello", "world"] - toCharArray()
- 문자열을 문자 배열로 변환합니다.
val str = "hello" val charArray = str.toCharArray() println(charArray.joinToString()) // "h, e, l, l, o"
문자열 검사 함수
- contains(substring: CharSequence)
- 문자열이 특정 문자열을 포함하는지 여부를 반환합니다.
val str = "hello world" val containsWorld = str.contains("world") println(containsWorld) // true - startsWith(prefix: String)
- 문자열이 특정 문자열로 시작하는지 여부를 반환합니다.
val str = "hello world" val startsWithHello = str.startsWith("hello") println(startsWithHello) // true - endsWith(suffix: String)
- 문자열이 특정 문자열로 끝나는지 여부를 반환합니다.
val str = "hello world" val endsWithWorld = str.endsWith("world") println(endsWithWorld) // true - isEmpty()
- 문자열이 비어있는지 여부를 반환합니다.
val str = "" val isEmptyStr = str.isEmpty() println(isEmptyStr) // true - isBlank()
- 문자열이 공백이거나 비어있는지 여부를 반환합니다.
val str = " " val isBlankStr = str.isBlank() println(isBlankStr) // true
문자열 변환 함수
- toInt() / toDouble() / toLong() / toFloat()
- 문자열을 특정 숫자 타입으로 변환합니다.
val str = "123" val intVal = str.toInt() println(intVal) // 123 - toBoolean()
- 문자열을 Boolean 타입으로 변환합니다.
val str = "true" val boolVal = str.toBoolean() println(boolVal) // true
예제 문제 해결
이제, 이러한 함수를 활용하여 주어진 문제를 해결하는 방법을 다시 살펴보겠습니다. 여기서 우리는 replace와 uppercaseChar를 사용할 수 있습니다.
class Solution {
fun solution(my_string: String, alp: String): String {
return my_string.replace(alp, alp.toUpperCase())
}
또한, map과 uppercaseChar를 사용할 수 있습니다.
class Solution {
fun solution(my_string: String, alp: String): String {
return my_string.map {
if (it.toString() == alp) it.uppercaseChar() else it
}.joinToString("")
}
}
이 외에도 다양한 방법으로 문제를 해결할 수 있으며, 각 함수의 특성을 잘 이해하고 활용하면 더욱 효율적인 코드를 작성할 수 있습니다.
'Kotlin' 카테고리의 다른 글
| [Kotlin] 코틀린의 클래스(Class), 생성자(Constructor), 초기화 블록(Initializer blocks), 인스턴스(Instance) 사용법 (2) | 2024.07.22 |
|---|---|
| [Kotlin] 보조 생성자(secondary constructors)에서의 초기화 블록(initializer blocks) 실행 타이밍 (0) | 2024.07.22 |
| [Kotlin] 코틀린 자료 구조와 Java와의 관계 (0) | 2024.07.05 |
| [Kotlin] 코틀린 기본 입출력 - 코틀린 코테 준비 (0) | 2024.07.05 |
| [Kotlin] 💗 기본 문법 정리 - 코틀린 코테 준비 (0) | 2024.07.05 |