文字列の指定した位置の文字を取り出す String.charAt
ひとことで言うと
指定した位置にある文字を返します。
ブロックとコード
文字列""の○番目の文字
ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。
"".charAt(0)"".char_at(0)引数(ひきすう)
引数とは、ブロックの中で自分で選んだり入力したりする部分のことです。
| 名前 | 選べる値 | 意味 |
|---|---|---|
| index | 数値 | 目的の文字の位置を 0 から始まる値で指定します。 |
使用例
You can find out which character is at any place in some text. You might have text thats says "Hello there!". The character at position 6 is 't'. The word "Hello" plus the space have positions 0 - 5, so, 't' is at position 6. To get the character at this position, the letter 't', you could use a blocks like this:
JavaScript
let greeting = "Hello there!";
let tee = greeting.charAt(6);Python
greeting = "Hello there!"
tee = greeting.char_at(6)Make a funny new sentence from an existing sentence by choosing every other letter.
JavaScript
let sentence = "Cinnamon vanilla latte";
let funnySentence = "";
for (let i = 0; i < sentence.length; i++) {
if (i % 2 > 0) {
funnySentence = funnySentence + sentence.charAt(i);
}
}Python
sentence = "Cinnamon vanilla latte"
funnySentence = ""
for i in range(len(sentence)):
if i % 2 > 0:
funnySentence = funnySentence + sentence.char_at(i)