亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

Kotlin 字符串替換

Kotlin中的字符串替換方法是 String.replace(oldValue,newValue)。 ignoreCase 是一個(gè)可選參數(shù),可以作為replace()方法第三個(gè)參數(shù)。 在本教程中,我們將通過(guò)示例說(shuō)明對(duì)于字符串中出現(xiàn)的每個(gè) oldValue,我們將用一個(gè)新的值(另一個(gè)字符串)替換一個(gè)舊的值(String),以及忽略和不忽略 oldValue 的字符大小寫(xiě)用法。

語(yǔ)法

String.replace方法的語(yǔ)法為:

String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String

OldValue - 字符串中每次出現(xiàn) oldValue 都必須替換為 newValue 的字符串。

ignoreCase - [可選] 如果為true,則在String中查找匹配項(xiàng)時(shí)將不考慮 oldValue 的字符大小寫(xiě)。 如果為false,則在字符串中查找 oldValue 的匹配項(xiàng)時(shí)將區(qū)分字符大小寫(xiě)。 ignoreCase的默認(rèn)值為 false。

Kotlin 替換子字符串,區(qū)分大小寫(xiě)

fun main(args: Array<String>) {
 
    var str = "Kotlin Tutorial - Replace String - Programs"
    val oldValue = "Programs"
    val newValue = "Examples"
 
    val output = str.replace(oldValue, newValue)
 
    print(output)
}

輸出結(jié)果:

Kotlin Tutorial - Replace String - Examples

Kotlin 替換子字符串,不區(qū)分大小寫(xiě)

fun main(args: Array<String>) {
 
    var str = "Kotlin Tutorial - Replace String - Programs"
    val oldValue = "PROGRAMS"
    val newValue = "Examples"
 
    val output = str.replace(oldValue, newValue, ignoreCase = true)
 
    print(output)
}

輸出結(jié)果:

Kotlin Tutorial - Replace String - Examples

在此Kotlin教程– Kotlin替換字符串中,我們學(xué)習(xí)了如何在字符串中用 新值 替換 舊值。 以及Kotlin示例替換字符串時(shí)忽略大小寫(xiě)的問(wèn)題。