戻る

slice
文字列から特定の範囲を抜き出す関数

[サンプル]

str.slice(startIndex[, endIndex])
startIndex開始するインデックス(インデックスは0から始まる整数)必須
endIndex終了するインデックス任意(指定した位置の手前まで)省略すると文字列末尾までを返します。
戻り値指定した範囲の文字列元の文字列を変更しない新しい文字列を返します

copy
let tmp = "0123456789";
console.log(tmp.slice(0));//0123456789
console.log(tmp.slice(0,0));//空
console.log(tmp.slice(0,1));//0
console.log(tmp.slice(0,2));//01
console.log(tmp.slice(9));//9
console.log(tmp.slice(9,9));//空
console.log(tmp.slice(9,10));//9
console.log(tmp.slice(9,11));//9
console.log(tmp.slice(10));//空
console.log(tmp.slice(-1));//9
console.log(tmp.slice(-9));//123456789
console.log(tmp.slice(-10));//0123456789
console.log(tmp.slice(2,1));//空(start ≥ endのため)

let tmp2 = "abcdef";
tmp2 = tmp2.slice(0,2);
console.log("tmp2:"+tmp2);//tmp2:ab
※console.logの参照方法
ブラウザを右クリックします。
メニューリストから一番下の検証を選択します。
デフォルトで一番下のタブにConsoleとあります。
このConsoleタブを選択するとconsole.logの結果を確認できます。

tmp.slice(0)

第2引数がないため、
インデックス位置0から全てを返します。
tmp.slice(0,0)
第2引数がある場合は、一つ手前までの範囲を取得するため
開始のインデックス位置と同じということは、空文字になります。

tmp.slice(0,1)
インデックス位置0から一つ手前の0まで。
すなわち、0を返します。

tmp.slice(-1)
負の数を引数としている場合は、後ろから指定した文字数を返します。

tmp.slice(2,1)
開始インデックス位置 ≥ 終了インデックス位置のため
取得できる範囲外となり、空文字を返します。



戻る
back

slice
Function to extract a specific range from a string

[sample]

str.slice(startIndex[, endIndex])
startIndexStarting index (index is an integer starting from 0)Required
endIndexEnding indexOptional (up to the specified position)If omitted, returns up to the end of the string.
ReturnsThe specified range of textReturns a new string that does not modify the original text

copy
let tmp = "0123456789";
console.log(tmp.slice(0));//0123456789
console.log(tmp.slice(0,0));//empty string
console.log(tmp.slice(0,1));//0
console.log(tmp.slice(0,2));//01
console.log(tmp.slice(9));//9
console.log(tmp.slice(9,9));//empty string
console.log(tmp.slice(9,10));//9
console.log(tmp.slice(9,11));//9
console.log(tmp.slice(10));//empty string
console.log(tmp.slice(-1));//9
console.log(tmp.slice(-9));//123456789
console.log(tmp.slice(-10));//0123456789
console.log(tmp.slice(2,1));//empty string(Because start ≥ end)

let tmp2 = "abcdef";
tmp2 = tmp2.slice(0,2);
console.log("tmp2:"+tmp2);//tmp2:ab
*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.

tmp.slice(0)

Since there is no second argument,
it returns everything from index 0.
tmp.slice(0,0)
If there is a second argument, it retrieves the range up to the previous index.
If the second argument is the same as the starting index, it returns an empty string.

tmp.slice(0,1)
From index 0 to the previous 0.
In other words, it returns 0.

tmp.slice(-1)
If a negative number is specified as an argument, the specified number of characters from the end is returned.

tmp.slice(2,1)
Because the start index is greater than or equal to the end index, it is outside the range that can be retrieved and an empty string is returned.



back



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