Kotlin

[Kotiln] 코틀린 문자열 다루기

yujinius 2024. 7. 5. 22:34

문자열 변환 함수

  1. toUpperCase() / uppercase()
    • 문자열의 모든 문자를 대문자로 변환합니다.
    val str = "hello"
    val upperStr = str.toUpperCase() // 또는 str.uppercase()
    println(upperStr) // "HELLO"
    
  2. toLowerCase() / lowercase()
    • 문자열의 모든 문자를 소문자로 변환합니다.
    val str = "HELLO"
    val lowerStr = str.toLowerCase() // 또는 str.lowercase()
    println(lowerStr) // "hello"
    
  3. capitalize()
    • 문자열의 첫 글자를 대문자로 변환합니다.
    val str = "hello"
    val capitalizedStr = str.capitalize()
    println(capitalizedStr) // "Hello"
    
  4. decapitalize()
    • 문자열의 첫 글자를 소문자로 변환합니다.
    val str = "Hello"
    val decapitalizedStr = str.decapitalize()
    println(decapitalizedStr) // "hello"
    

문자열 조작 함수

  1. replace(oldValue: String, newValue: String)
    • 문자열 내의 특정 문자열을 다른 문자열로 치환합니다.
    val str = "hello world"
    val replacedStr = str.replace("world", "Kotlin")
    println(replacedStr) // "hello Kotlin"
    
  2. replaceFirst(oldValue: String, newValue: String)
    • 문자열 내에서 첫 번째로 매칭되는 특정 문자열을 다른 문자열로 치환합니다.
    val str = "hello world world"
    val replacedFirstStr = str.replaceFirst("world", "Kotlin")
    println(replacedFirstStr) // "hello Kotlin world"
    
  3. substring(startIndex: Int, endIndex: Int)
    • 문자열의 특정 부분을 추출합니다.
    val str = "hello world"
    val substr = str.substring(0, 5)
    println(substr) // "hello"
    
  4. trim()
    • 문자열의 양 끝 공백을 제거합니다.
    val str = "  hello world  "
    val trimmedStr = str.trim()
    println(trimmedStr) // "hello world"
    
  5. trimStart() / trimEnd()
    • 문자열의 앞쪽 또는 뒤쪽 공백을 제거합니다.
    val str = "  hello world  "
    val trimmedStartStr = str.trimStart()
    println(trimmedStartStr) // "hello world  "
    
    val trimmedEndStr = str.trimEnd()
    println(trimmedEndStr) // "  hello world"
    
  6. split(delimiter: String)
    • 문자열을 특정 구분자로 분리하여 리스트로 반환합니다.
    val str = "hello world"
    val splitStr = str.split(" ")
    println(splitStr) // ["hello", "world"]
    
  7. toCharArray()
    • 문자열을 문자 배열로 변환합니다.
    val str = "hello"
    val charArray = str.toCharArray()
    println(charArray.joinToString()) // "h, e, l, l, o"
    

문자열 검사 함수

  1. contains(substring: CharSequence)
    • 문자열이 특정 문자열을 포함하는지 여부를 반환합니다.
    val str = "hello world"
    val containsWorld = str.contains("world")
    println(containsWorld) // true
    
  2. startsWith(prefix: String)
    • 문자열이 특정 문자열로 시작하는지 여부를 반환합니다.
    val str = "hello world"
    val startsWithHello = str.startsWith("hello")
    println(startsWithHello) // true
    
  3. endsWith(suffix: String)
    • 문자열이 특정 문자열로 끝나는지 여부를 반환합니다.
    val str = "hello world"
    val endsWithWorld = str.endsWith("world")
    println(endsWithWorld) // true
    
  4. isEmpty()
    • 문자열이 비어있는지 여부를 반환합니다.
    val str = ""
    val isEmptyStr = str.isEmpty()
    println(isEmptyStr) // true
    
  5. isBlank()
    • 문자열이 공백이거나 비어있는지 여부를 반환합니다.
    val str = "  "
    val isBlankStr = str.isBlank()
    println(isBlankStr) // true
    

문자열 변환 함수

  1. toInt() / toDouble() / toLong() / toFloat()
    • 문자열을 특정 숫자 타입으로 변환합니다.
    val str = "123"
    val intVal = str.toInt()
    println(intVal) // 123
    
  2. 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("")
    }
}

이 외에도 다양한 방법으로 문제를 해결할 수 있으며, 각 함수의 특성을 잘 이해하고 활용하면 더욱 효율적인 코드를 작성할 수 있습니다.