Roughly cover the materials in Chapters 7 and 8.
String
is the data type for text.
Char
"Text"
and "t"
are String
.'T'
and 't'
are Char
..length
$val
$var
${expression}
str[i]
: Access the i
-th Char
in a String
type variable/value str
.
Char
to represent '
'''
does not work!'\''
String
to represent ""
""""
does not work!"\"\""
Char
to represent \
'\'
does not work!'\\'
String
to represent $
"\$"
\'
: Single quote '
\"
: Double quote\\
: Backslash\$
: Dollar sign\t
: Tab\b
: Backspace\n
: Newline\r
: Carriage return\uXXXX
: Unicode, see Wikipedia.substring
start: Int
start: Int, end: Int
range: IntRange
indexOf
char: Char
lastIndexOf
char: Char
indexOfFirst
predicate: (Char) -> Boolean
indexOfLast
predicate: (Char) -> Boolean
split
[i]
to access the i
-th element.replace
(oldChar: Char, newChar: Char)
(oldString: String, newString: String)
(regex: Regex, replacement: String)
(regex: Regex, transform: (MatchResult) -> CharSequence)
toInt()
toIntOrNull()
toDouble()
toDoubleOrNull()
toLowerCase()
is not a type conversion.leet(str: String): String
,將字串中正常的英文字母,轉換成非英文字母的 leet 寫法。只要換出來沒英文就可以。fun wordCount(str: String): Int
,計算 str
之中有多少個英文字(Word),相同的英文字如果出現數次,按出現次數計算。保證 str
中只有英文字母、空白(' '
)、逗號(','
)、句號('.'
)。fun number(str: String): Int?
,將英文字 str
換算成對應的整數值,如 one
將換成 1
, zero
換成 0
。只需要處理零到九十九萬九千九百九十九之間的整數即可。輸入可能含有大寫字母,函數必須無視大小寫。如果 str
並非一個英文數字,請回傳 null
。Integers
Byte
-128
to 127
Short
-32768
to 32767
Int
-2147483648
to 2147483647
Long
-9223372036854775808
to 9223372036854775807
Float
: 32-bit (Single precision)
Double
: 64-bit (Double precision)
String
to number
.toByte()
, .toShort()
, toInt()
, toLong()
, toFloat()
, toDouble()
..toByteOrNull()
, .toShortOrNull()
, …Int
to Double
1/2
is 0
0.0
1.0
.toDouble()
Double
to Int
.toInt()
1.9.toInt()
and -1.9.toInt()
.roundToInt()
import kotlin.math.roundToInt
before use1.5.roundToInt()
2.5.roundToInt()
-0.5.roundToInt()
-1.5.roundToInt()
String
.toString()
radix=2
means binary, and radix=10
means decimal.
radix
is 10
"$val"
"%.3f".format(0.12345678)
"%.4f".format(9.87654321)