Java 與 Kotlin 中的字串
本指南包含如何在 Java 和 Kotlin 中執行常見字串任務的範例。這將幫助您從 Java 遷移到 Kotlin,並以純正的 Kotlin 方式編寫程式碼。
串接字串
在 Java 中,您可以透過以下方式來執行此操作:
// Java
String name = "Joe";
System.out.println("Hello, " + name);
System.out.println("Your name is " + name.length() + " characters long");
在 Kotlin 中,在變數名稱前使用錢號符號 ($
),將此變數的值插入到您的字串中:
fun main() {
// Kotlin
val name = "Joe"
println("Hello, $name")
println("Your name is ${name.length} characters long")
}
您可以透過將其用花括號(例如 ${name.length}
)括起來,來插入複雜表達式的值。有關更多資訊,請參閱 字串範本。
建構字串
在 Java 中,您可以使用 StringBuilder:
// Java
StringBuilder countDown = new StringBuilder();
for (int i = 5; i > 0; i--) {
countDown.append(i);
countDown.append("
");
}
System.out.println(countDown);
在 Kotlin 中,使用 buildString() — 這是一個接受用於建構字串的邏輯作為 lambda 引數的 內聯函數:
fun main() {
// Kotlin
val countDown = buildString {
for (i in 5 downTo 1) {
append(i)
appendLine()
}
}
println(countDown)
}
在底層,buildString
使用與 Java 相同的 StringBuilder
類別,並且您可以在 lambda 內部透過隱式的 this
存取它。
進一步了解 lambda 編碼慣例。
從集合項目建立字串
在 Java 中,您使用 Stream API 來過濾、映射,然後收集項目:
// Java
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6);
String invertedOddNumbers = numbers
.stream()
.filter(it -> it % 2 != 0)
.map(it -> -it)
.map(Object::toString)
.collect(Collectors.joining("; "));
System.out.println(invertedOddNumbers);
在 Kotlin 中,使用 joinToString() 函數,這是 Kotlin 為每個 List 定義的函數:
fun main() {
// Kotlin
val numbers = listOf(1, 2, 3, 4, 5, 6)
val invertedOddNumbers = numbers
.filter { it % 2 != 0 }
.joinToString(separator = ";") {"${-it}"}
println(invertedOddNumbers)
}
在 Java 中,如果您希望分隔符號和其後的項目之間有空格,您需要明確地在分隔符號中加入空格。
進一步了解 joinToString() 的用法。
如果字串為空字元串,則設定預設值
在 Java 中,您可以使用 三元運算子:
// Java
public void defaultValueIfStringIsBlank() {
String nameValue = getName();
String name = nameValue.isBlank() ? "John Doe" : nameValue;
System.out.println(name);
}
public String getName() {
Random rand = new Random();
return rand.nextBoolean() ? "" : "David";
}
Kotlin 提供了 ifBlank() 內聯函數,它接受預設值作為引數:
// Kotlin
import kotlin.random.Random
fun main() {
val name = getName().ifBlank { "John Doe" }
println(name)
}
fun getName(): String =
if (Random.nextBoolean()) "" else "David"
取代字串開頭和結尾的字元
在 Java 中,您可以使用 replaceAll() 函數。在此情況下,replaceAll()
函數接受正規表達式 ^##
和 ##$
,它們分別定義了以 ##
開始和結束的字串:
// Java
String input = "##place##holder##";
String result = input.replaceAll("^##|##$", "");
System.out.println(result);
在 Kotlin 中,使用 removeSurrounding() 函數,並將字串分隔符號設為 ##
:
fun main() {
// Kotlin
val input = "##place##holder##"
val result = input.removeSurrounding("##")
println(result)
}
取代出現的內容
在 Java 中,您可以使用 Pattern 和 Matcher 類別,例如,用來混淆一些資料:
// Java
String input = "login: Pokemon5, password: 1q2w3e4r5t";
Pattern pattern = Pattern.compile("\\w*\\d+\\w*");
Matcher matcher = pattern.matcher(input);
String replacementResult = matcher.replaceAll(it -> "xxx");
System.out.println("Initial input: '" + input + "'");
System.out.println("Anonymized input: '" + replacementResult + "'");
在 Kotlin 中,您使用 Regex 類別,這簡化了正規表達式的使用。此外,使用 多行字串 可以透過減少反斜線的數量來簡化正規表達式模式:
fun main() {
// Kotlin
val regex = Regex("""\w*\d+\w*""") // multiline string
val input = "login: Pokemon5, password: 1q2w3e4r5t"
val replacementResult = regex.replace(input, replacement = "xxx")
println("Initial input: '$input'")
println("Anonymized input: '$replacementResult'")
}
分割字串
在 Java 中,若要使用句號字元 (.
) 分割字串,您需要使用遮蔽 (\\
)。這是因為 String
類別的 split() 函數接受正規表達式作為引數:
// Java
System.out.println(Arrays.toString("Sometimes.text.should.be.split".split("\\.")));
在 Kotlin 中,使用 Kotlin 函數 split(),它接受不定數量 (varargs) 的分隔符號作為輸入參數:
fun main() {
// Kotlin
println("Sometimes.text.should.be.split".split("."))
}
如果您需要使用正規表達式進行分割,請使用接受 Regex
作為參數的重載版 split()
。
截取子字串
在 Java 中,您可以使用 substring() 函數,它接受一個字元(用於開始截取子字串)的包含性起始索引。若要在此字元之後截取子字串,您需要增加索引:
// Java
String input = "What is the answer to the Ultimate Question of Life, the Universe, and Everything? 42";
String answer = input.substring(input.indexOf("?") + 1);
System.out.println(answer);
在 Kotlin 中,您使用 substringAfter() 函數,並且不需要計算您想在其後截取子字串的字元索引:
fun main() {
// Kotlin
val input = "What is the answer to the Ultimate Question of Life, the Universe, and Everything? 42"
val answer = input.substringAfter("?")
println(answer)
}
此外,您可以在字元的最後一次出現之後截取子字串:
fun main() {
// Kotlin
val input = "To be, or not to be, that is the question."
val question = input.substringAfterLast(",")
println(question)
}
使用多行字串
在 Java 15 之前,有幾種方法可以建立多行字串。例如,使用 String
類別的 join() 函數:
// Java
String lineSeparator = System.getProperty("line.separator");
String result = String.join(lineSeparator,
"Kotlin",
"Java");
System.out.println(result);
在 Java 15 中,文字區塊 出現了。有一點需要記住:如果您印出多行字串,並且三引號在下一行,則會多出一個空行:
// Java
String result = """
Kotlin
Java
""";
System.out.println(result);
輸出:
如果您將三引號放在與最後一個單字相同的行上,這種行為差異就會消失。
在 Kotlin 中,您可以將行與引號格式化在新行上,並且輸出中不會有額外的空行。任何行的最左側字元標識該行的開頭。與 Java 的不同之處在於,Java 會自動修剪縮排,而在 Kotlin 中您應該明確地執行此操作:
fun main() {
// Kotlin
val result = """
Kotlin
Java
""".trimIndent()
println(result)
}
輸出:
若要有多一個空行,您應該明確地將此空行加入到您的多行字串中。
在 Kotlin 中,您還可以使用 trimMargin() 函數來自訂縮排:
// Kotlin
fun main() {
val result = """
# Kotlin
# Java
""".trimMargin("#")
println(result)
}
進一步了解 多行字串。
接下來做什麼?
- 瀏覽其他 Kotlin 慣用語。
- 學習如何使用 Java 到 Kotlin 轉換器 將現有的 Java 程式碼轉換為 Kotlin。
如果您有喜歡的慣用語,歡迎您透過傳送 Pull Request 來分享。