0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScript slice() 文字列から好きな場所だけ切り取る

Last updated at Posted at 2022-04-17

用途

文字列から好きな場所だけ切り取りたい時に使う。

使用方法

書き方
var hoge = "文字列";
hoge.slice( 開始位置, 終了位置 ); // 終了位置は省略可能
例(終了位置を指定)
var hoge = "ABCDEFGHI";
var hogehoge = hoge.slice(3, 5);
console.log(hogehoge);

// 結果
// DE
例(終了位置を指定しない)
var hoge = "ABCDEFGHI";
var hogehoge = hoge.slice(3);
console.log(hogehoge);

// 結果
// DEFGHI
// ※開始位置以降全て取得する
例(開始位置がマイナス)
var hoge = "ABCDEFGHI";
var hogehoge = hoge.slice(-2);
console.log(hogehoge);

// 結果
// HI

引数マイナスについて
末尾が -1 から始まる

A B C D E F G H I
-9 -8 -7 -6 -5 -4 -3 -2 -1
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?