padStart
所在位置是kotlin.text.padStart
,其相關用法介紹如下。
用法一
返回一個字符序列,該字符序列的內容在開頭填充到指定的length,並帶有指定的字符或空格。
例子:
import java.util.Locale
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val padWithSpace = "125".padStart(5)
println("'$padWithSpace'") // ' 125'
val padWithChar = "a".padStart(5, '.')
println("'$padWithChar'") // '....a'
// string is returned as is, when its length is greater than the specified
val noPadding = "abcde".padStart(3)
println("'$noPadding'") // 'abcde'
//sampleEnd
}
輸出:
' 125' '....a' 'abcde'
參數
padChar
- 填充字符串的字符,如果它的長度小於長度指定的。默認使用空間。
返回返回長度至少為 length 的字符序列,該序列由前麵加上 padChar 的 this
字符序列組成,次數達到該長度所需的次數。
用法二
用指定的字符或空格將字符串填充到指定的length 開頭。
例子:
import java.util.Locale
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val padWithSpace = "125".padStart(5)
println("'$padWithSpace'") // ' 125'
val padWithChar = "a".padStart(5, '.')
println("'$padWithChar'") // '....a'
// string is returned as is, when its length is greater than the specified
val noPadding = "abcde".padStart(3)
println("'$noPadding'") // 'abcde'
//sampleEnd
}
輸出:
' 125' '....a' 'abcde'
參數
padChar
- 填充字符串的字符,如果它的長度小於長度指定的。默認使用空間。
返回返回長度至少為 length 的字符串,該字符串由前麵加上 padChar 的 this
字符串組成,達到該長度所需的次數。
相關用法
- Kotlin padEnd用法及代碼示例
- Kotlin partition用法及代碼示例
- Kotlin plus用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
- Kotlin ifBlank用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin getOrPut用法及代碼示例
- Kotlin Triple.<init>用法及代碼示例
- Kotlin Duration.toString用法及代碼示例
- Kotlin windowed用法及代碼示例
- Kotlin indexOf用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.text.padStart。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。