戻る

substring()
substr()は非推奨です

substring関数は文字列を抽出する機能です。
開始位置0から始まり終了位置よりも小さい整数のインデックス位置
終了位置開始位置よりも大きい整数のインデックス位置(範囲は終了インデックス位置-1)
インデックス位置とは、文字列の左から1個目の文字を0としてカウントする位置を示します。
(例)
データ補足
abc対象文字列
123何個目か?(1から始まる個数)a:1 b:2 c:3
012インデックス位置(0から始まる整数)a:0 b:1 c:2
substringの関数をインデックス位置の範囲を指定して文字列の抽出をする関数となります。
下記は実際のサンプルです。
※console.logの参照方法
ブラウザを右クリックします。
メニューリストから一番下の検証を選択します。
デフォルトで一番下のタブにConsoleとあります。
このCosoleタブを選択するとconsole.logの結果を確認できます。

[サンプル]
copy
let data ="0123456789";
console.log("テスト文字列の個数");
console.log("data.length:"+data.length);//10
console.log("開始(インデックス位置0から抽出した例)");
console.log("0から0の範囲を抽出:" + data.substring(0,0));//空文字
console.log("0から1の範囲を抽出:" + data.substring(0,1));//0
console.log("0から2の範囲を抽出:" + data.substring(0,2));//01
console.log("0から8の範囲を抽出:" + data.substring(0,8));//01234567
console.log("0から9の範囲を抽出:" + data.substring(0,9));//012345678
console.log("0から10の範囲を抽出:" + data.substring(0,10));//012345679
console.log("0から11の範囲を抽出:" + data.substring(0,11));//012345679
console.log("第2引数にマイナスを使用した例");
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(0,-1));//空文字
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(1,-1));//0
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(2,-1));//01
console.log("第1引数にマイナスを使用した例");
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(-1,0));//空文字
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(-1,1));//0
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(-1,2));//01
console.log("引数がともにマイナスを使用した例");
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(-1,-1));//空文字
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(-1,-2));//空文字
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(-2,-2));//空文字
console.log("第1引数 > 第2引数の例");
console.log("開始からインデックス位置0の範囲を抽出:" + data.substring(4,2));//23
console.log("引数に整数以外を使用した例");
let tmp = "abc";
//下記エラーが発生します
console.log("Uncaught SyntaxError: missing ) after argument list (at file name:row:col)");
//console.log(tmp = tmp.substring(0,"1");
//console.log(tmp = tmp.substring(0,"a");
//console.log(tmp = tmp.substring(0,"abc");

data.substring(0,11)
範囲外のインデックス位置を使用した例
使用可能なインデックス位置に修正するようです。

data.substring(0,-1)
第2引数にマイナスを使用した例
第1引数と第2引数を入れ替えているようです。
第2引数が負の整数である場合は0に値を補正してしまうようです。
[手順]
0と-1を入れ替えます。
[入れ替えた結果]
第1引数:-1
第2引数:0
第1引数が負の整数のため0に変更します。
[第1引数を変更した結果]
第1引数:0
第2引数:0
[substringで抽出した結果]
0から0は文字列としての範囲がないため、空文字を返します。

第1引数にマイナスを使用した例も考え方は上記と同様のように第1引数を0として考えるようです。

data.substring(4,2)
第1引数 > 第2引数(第2引数が第1引数よりも小さいケース)
第1引数と第2引数を入れ替えます
[入れ替えた結果]
第1引数:2
第2引数:4
[対象文字列]0123456789
よって取得する文字列は「23」となります。

引数がともにマイナスを使用した例
両方の引数に0は入った状態となるため、抽出する対象がなく、空文字が返ります。



戻る
back

substring()
substr() is deprecated

The substring function extracts a string.
Start positionAn integer index position starting from 0 and less than the end position
End positionAn integer index position greater than the start position (range is end index position - 1)
The index position indicates the position in the string, counting from the left as 0, with the first character counting as 0.
(Example)
DataAdditional Information
abcTarget String
123Which Item? (Number starting from 1) a:1 b:2 c:3
012Index Position (Integer starting from 0) a:0 b:1 c:2
This function uses the substring function to extract strings by specifying a range of index positions.
Below is an actual example.
*How to view console.log
Right-click in the browser.
Select Validate at the bottom of the menu list.
By default, the bottom tab is labeled Console.
Select this tab to view the console.log results.

[Sample]
copy
let data ="0123456789";
console.log("Number of test strings");
console.log("data.length:" + data.length); //10
console.log("Start (Example of extracting from index 0)");
console.log("Extract the range from 0 to 0:" + data.substring(0,0)); //Empty string
console.log("Extract the range from 0 to 1:" + data.substring(0,1)); //0
console.log("Extract the range from 0 to 2:" + data.substring(0,2)); //01
console.log("Extract the range from 0 to 8:" + data.substring(0,8)); //01234567
console.log("Extract the range from 0 to 9:" + data.substring(0,9)); //012345678
console.log("Extract the range from 0 to 10:" + data.substring(0,10));//012345679
console.log("Extracting the range from 0 to 11:" + data.substring(0,11));//012345679
console.log("Example of using a minus as the second argument");
console.log("Extracting the range from the start to index position 0:" + data.substring(0,-1));//empty string
console.log("Extracting the range from the start to index position 0:" + data.substring(1,-1));//0
console.log("Extracting the range from the start to index position 0:" + data.substring(2,-1));//01
console.log("Example of using a minus as the first argument");
console.log("Extracting the range from the start to index position 0:" + data.substring(-1,0));//empty string
console.log("Extracting the range from the start to index position 0:" + data.substring(-1,1)); //0
console.log("Extracting the range from the start to index position 0:" + data.substring(-1,2)); //01
console.log("Example where both arguments are negative");
console.log("Extracting the range from the start to index position 0:" + data.substring(-1,-1)); //empty string
console.log("Extracting the range from the start to index position 0:" + data.substring(-1,-2)); //empty string
console.log("Extracting the range from the start to index position 0:" + data.substring(-2,-2)); //empty string
console.log("Example where the first argument is greater than the second argument");
console.log("Extracting the range from the start to index position 0:" + data.substring(4,2)); //23
console.log("Example where arguments are non-integer");
let tmp = "abc";
//The following error occurs:
console.log("Uncaught SyntaxError: missing ) after argument list (at file name:row:col)");
//console.log(tmp = tmp.substring(0,"1");
//console.log(tmp = tmp.substring(0,"a");
//console.log(tmp = tmp.substring(0,"abc");

data.substring(0,11)
Example of using an out-of-range index position
It appears to correct to a usable index position.

data.substring(0,-1)
Example of using a negative second argument
It appears to swap the first and second arguments.
If the second argument is a negative integer, it appears to correct the value to 0.
[Procedure]
Swap 0 and -1.
[Result of swap]
First argument: -1
Second argument: 0
Since the first argument is a negative integer, it is changed to 0.
[Result of changing the first argument]
First argument: 0
Second argument: 0
[Result of extracting with substring]
Since there is no range for strings from 0 to 0, an empty string is returned.

In the example where the first argument is a negative number, the first argument is treated as 0, similar to the above.

data.substring(4,2)
First argument > Second argument (when the second argument is smaller than the first argument)
Swap the first and second arguments
[Swap result]
First argument: 2
Second argument: 4
[Target string]0123456789
Therefore, the string obtained is "23."

Example using negative values for both arguments
Since both arguments contain 0, there is nothing to extract, and an empty string is returned.



back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。