文字列の一部を取り出す String.substr
ひとことで言うと
現在の文字列の部分文字列を返します。
ブロックとコード
文字列""の0番目から0文字
ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。
"".substr(0, 0)"".substr(0, 0)引数(ひきすう)
引数とは、ブロックの中で自分で選んだり入力したりする部分のことです。
| 名前 | 選べる値 | 意味 |
|---|---|---|
| start | 数値 | 最初の文字の位置。負の値を指定すると、末尾から数えることができます。たとえば:0 |
| length | 数値 | 抽出する文字数 |
使用例
You could make a new string that just has the word "there" from a bigger string that says "Hello there!!!". To do that, the substring is copied from the character position of 6 in the first string and 5 characters are copied. It's done like this:
JavaScript
let there = "Hello there!!!".substr(6, 5)Python
there = "Hello there!!!".substr(6, 5)If you want to have the substring copy to the end of the first string, you just use a starting position and either don't use the length parameter or set it to 0. This will copy all of the first string beginning at position 6. The substring will say "there!!!" in this case.
JavaScript
let there = "Hello there!!!".substr(6)
// - or -
let there = "Hello there!!!".substr(6, 0)Python
there = "Hello there!!!".substr(6)
# - or -
there2 = "Hello there!!!".substr(6, 0)Copy the nouns from the sentence into two smaller strings.
JavaScript
let sentence = "The mountains have snow"
let mountains = sentence.substr(4, 9)
let snow = sentence.substr(19)Python
sentence = "The mountains have snow"
mountains = sentence.substr(4, 9)
snow = sentence.substr(19)