どうも、ちょげ(@chogetarou)です。
slice()を使って文字列(string)の最初の文字を取得する方法を紹介します。
方法

slice()を使って文字列(string)の先頭の文字を取得するには、2つの引数を使います。
まず、文字列からslice()を呼び出します。
そして、slice()の第1引数に0、第2引数に1を指定します。
const first: string = text.slice(0, 1)
上記のslice()は、呼び出した文字列(string)の最初の文字を取得します。
使用例
const text: string = "Hello, TypeScript"
const first: string = text.slice(0, 1)
console.log(first)
出力:
H
コメント