Spec-Zone.ru › Kotlin 2

prefix

kotlin-stdlib/kotlin.text/HexFormat/NumberHexFormat/prefix

Начиная с Kotlin: 2.2

val prefix: String

Строка, которая непосредственно предшествует шестнадцатеричному представлению числового значения. По умолчанию — пустая строка.

При форматировании эта строка помещается перед шестнадцатеричным представлением. При разборе анализируемая строка должна начинаться с этой строки. Префикс разбирается без учёта регистра.

Примеры

import kotlin.test.*

fun main() { 
   //sampleStart 
   // By default, `prefix` is an empty string.
println(0x3a.toHexString()) // 0000003a
println("0000003a".hexToInt()) // 58

val prefixFormat = HexFormat { number.prefix = "0x" }

// `prefix` is placed before the hex representation.
println(0x3a.toHexString(prefixFormat)) // 0x0000003a
println("0x0000003a".hexToInt(prefixFormat)) // 58

// Parsing `prefix` is conducted in a case-insensitive manner.
println("0X0000003a".hexToInt(prefixFormat)) // 58

// Parsing fails if the input string does not start with the specified prefix.
// "0000003a".hexToInt(prefixFormat) // will fail with IllegalArgumentException 
   //sampleEnd
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.text/-hex-format/-number-hex-format/prefix.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API