1
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.

split() 文字列を分割 JavaScript

Posted at

用途

文字列を分割して使いたい時に使用する。

使用方法

書き方
var hoge = "文字列";
hoge.split(区切り文字や正規表現, 分割数);

文字列を任意の場所で分割することができる。分割数は省略できる。

var hoge = "01-02-03-04-05-06";
var array_hoge = hoge.split('-', 4);

console.log(array_hoge);
// ["01","02","03","04"]
応用
var hoge1 = "2000-01-02";
var hoge2 = hoge1.split('-')[0]; // 「-」で区切って配列に入れた後の1番目の文字列 
var hoge3 = hoge1.split('-')[1]; // 「-」で区切って配列に入れた後の2番目の文字列 
var hoge4 = hoge1.split('-')[2]; // 「-」で区切って配列に入れた後の3番目の文字列 

console.log(hoge1);
// 2000-01-01
console.log(hoge2);
// 2000
console.log(hoge3);
// 01
console.log(hoge4);
// 02
1
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
1
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?