Kotlin Standard Library क्या है
In this page:
String Utilities
Standard library कई सुविधाजनक string functions प्रदान करती है, जैसे किसी collection से एक string बनाने के लिए .joinToString(), और किसी string को दी गई संख्या में दोहराने के लिए .repeat()।
उदाहरण: String Utilities
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `fruits`, set to `listOf("apple", "banana", "cherry")`
val fruits = listOf("apple", "banana", "cherry")
// Print `fruits.joinToString(", ")` to the console, with a trailing newline
println(fruits.joinToString(", "))
// Print `"ab".repeat(3)` to the console, with a trailing newline
println("ab".repeat(3))
}
Login to try C/C++/Java/PHP code in the editor
Numeric Utilities
.coerceIn(min, max) जैसे functions किसी number को एक दी गई range तक clamp करते हैं, और .coerceAtLeast()/.coerceAtMost() इसे सिर्फ एक bound तक clamp करते हैं।
उदाहरण: Numeric Utilities
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `value`, set to `150`
val value = 150
// Print "Clamped: ${value.coerceIn(0, 100)}" to the console, with a trailing newline
println("Clamped: ${value.coerceIn(0, 100)}")
// Print "At most 50: ${value.coerceAtMost(50)}" to the console, with a trailing newline
println("At most 50: ${value.coerceAtMost(50)}")
}
Login to try C/C++/Java/PHP code in the editor
Collection Utilities Recap
Standard library के collection functions -- .map, .filter, .sorted, .groupBy, और बहुत कुछ -- manual loop के बिना रोज़ाना की data transformation ज़रूरतों के बड़े हिस्से को cover करते हैं।
उदाहरण: Collection Utilities Recap
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(5, 3, 8, 1, 9)`
val numbers = listOf(5, 3, 8, 1, 9)
// Print "Sorted: ${numbers.sorted()}" to the console, with a trailing newline
println("Sorted: ${numbers.sorted()}")
// Print "Max: ${numbers.max()}" to the console, with a trailing newline
println("Max: ${numbers.max()}")
// Print "Chunked: ${numbers.chunked(2)}" to the console, with a trailing newline
println("Chunked: ${numbers.chunked(2)}")
}
Login to try C/C++/Java/PHP code in the editor
What's Not in the Standard Library
Coroutines (kotlinx.coroutines), serialization (kotlinx.serialization), और date/time handling (kotlinx.datetime) सभी JetBrains द्वारा maintained अलग kotlinx libraries हैं, जिन्हें अपनी dependency चाहिए, भले ही वे language के स्वाभाविक विस्तार जैसी महसूस हों।
उदाहरण: What's Not in the Standard Library
fun main() {
// kotlin-stdlib alone is enough for everything in this example
val date = "2024-01-15"
val parts = date.split("-")
println("Year: ${parts[0]}, Month: ${parts[1]}, Day: ${parts[2]}")
}
Login to try C/C++/Java/PHP code in the editor
- दो numbers में max ढूंढने या किसी list को text में जोड़ने जैसी आम utilities को फिर से implement करना, जिन्हें standard library पहले से प्रदान करती है।
- यह मान लेना कि हर सुविधाजनक function (जैसे coroutines या serialization) standard library का हिस्सा है, जबकि कई अलग
kotlinxlibraries हैं जिन्हें अपनी dependency चाहिए। - जिन tasks को वे पहले से संक्षेप में हल करते हैं उनके लिए manual loops लिखने से पहले collection, string, और scope function utilities को explore न करना।
- Kotlin standard library (
kotlin-stdlib) compiler के साथ ship होती है और collections, string utilities, scope functions, और बहुत कुछ प्रदान करती है, बिना किसी अतिरिक्त dependency के। kotlinx.coroutines,kotlinx.serialization, औरkotlinx.datetimeजैसी libraries standard library से अलग हैं और explicitly जोड़नी चाहिए।- आम standard library helpers में
.joinToString(),.coerceIn(),.repeat(), और पहले कवर किए गए scope functions शामिल हैं। - पहले standard library जांचना अक्सर आम tasks के लिए verbose manual logic लिखने से बचाता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: