JavaScript文字列split()
例
単語を分割します。
let text = "How are you doing today?";
const myArray = text.split(" ");
単語を分割し、2番目の単語を返します。
let text = "How are you doing today?";
const myArray = text.split(" ");
let word = myArray[1];
スペースを含む文字を分割します。
const myArray = text.split("");
制限パラメーターを使用します。
const myArray = text.split(" ", 3);
以下のその他の例。
定義と使用法
このsplit()
メソッドは、文字列をサブ文字列の配列に分割します。
このsplit()
メソッドは新しい配列を返します。
このsplit()
メソッドは元の文字列を変更しません。
( "")が区切り文字として使用されている場合、文字列は単語間で分割されます。
構文
string.split(separator, limit)
パラメーター
Parameter | Description |
separator | Optional. A string or regular expression to use for splitting. If omitted, an array with the original string is returned. |
limit | Optional. An integer that limits the number of splits. Items after the limit are excluded. |
戻り値
タイプ | 説明 |
配列 | 分割された値を含む配列。 |
その他の例
文字列を文字に分割し、2番目の文字を返します。
const myArray = text.split("");
区切り文字として文字を使用します。
const myArray = text.split("o");
セパレータパラメータを省略すると、元の文字列を含む配列が返されます。
const myArray = text.split();
ブラウザのサポート
split()
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |