TypeScript 文字列(string)を1文字ずつループするサンプル
環境
Windows 11 Pro 64bit
TypeScript 4.4.4
構文
for (let char of 文字列) {
//ループ処理コード
//ループ変数「char」で文字を取得する
}
文字列(string)を1文字ずつループするには、「for…of」を使います。
使用例
const str = "Study";
for (let char of str) {
console.log(char);
}
const str = "Study";
for (let char of str) {
console.log(char);
}
const str = "Study"; for (let char of str) { console.log(char); }
実行結果
[LOG]: "S"
[LOG]: "t"
[LOG]: "u"
[LOG]: "d"
[LOG]: "y"
[LOG]: "S"
[LOG]: "t"
[LOG]: "u"
[LOG]: "d"
[LOG]: "y"
[LOG]: "S" [LOG]: "t" [LOG]: "u" [LOG]: "d" [LOG]: "y"