TypeScript  文字列(string)を1文字ずつループするサンプル

環境
Windows 11 Pro 64bit
TypeScript 4.4.4

構文
for (let char of 文字列) {
//ループ処理コード
//ループ変数「char」で文字を取得する
}
文字列(string)を1文字ずつループするには、「for…of」を使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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);
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[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"

 

TypeScript

Posted by arkgame