文字列の一部を取り出す String.substr

テキスト公式データ取得日:2026年11月13日動作確認待ち

ひとことで言うと

現在の文字列の部分文字列を返します。

ブロックとコード

ブロックの文言は公式の日本語版に合わせています(形は簡易表示です)。

引数(ひきすう)

引数とは、ブロックの中で自分で選んだり入力したりする部分のことです。

名前選べる値意味
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)
執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにMakeCodeを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む