Char

inline fun Char(code: Int): Char(source)

Creates a Char with the specified code, or throws an exception if the code is out of Char.MIN_VALUE.code..Char.MAX_VALUE.code.

If the program that calls this function is written in a way that only valid code is passed as the argument, using the overload that takes a UShort argument is preferable (Char(intValue.toUShort())). That overload doesn't check validity of the argument, and may improve program performance when the function is called routinely inside a loop.

Since Kotlin

1.5

Samples

import java.util.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) // will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) // will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF 
   //sampleEnd
}

expect fun Char(code: UShort): Char(source)

Creates a Char with the specified code.

Since Kotlin

1.5

Samples

import java.util.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) // will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) // will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF 
   //sampleEnd
}
actual inline fun Char(code: UShort): Char(source)

Creates a Char with the specified code.

Since Kotlin

1.5

Samples

import java.util.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) // will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) // will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF 
   //sampleEnd
}
actual inline fun Char(code: UShort): Char(source)

Creates a Char with the specified code.

Since Kotlin

1.5

Samples

import java.util.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) // will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) // will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF 
   //sampleEnd
}
actual inline fun Char(code: UShort): Char(source)

Creates a Char with the specified code.

Since Kotlin

1.8

Samples

import java.util.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) // will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) // will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF 
   //sampleEnd
}
actual inline fun Char(code: UShort): Char(source)

Creates a Char with the specified code.

Since Kotlin

1.8

Samples

import java.util.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) // will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) // will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF 
   //sampleEnd
}