split

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron+.

Podsumowanie

Splits a String object into an array of strings by separating the string into substrings.

Metoda obiektu: String
Zaimplementowana w: JavaScript 1.1, NES2.0
Wersja ECMA: ECMA-262 (jeśli separator jest ciągiem znaków)
ECMA-262, Edycja 3 (jeśli separator jest wyrażeniem regularnym)

Składnia

split([ separator ][, limit ])

Parametry

separator
Specifies the character to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string.
limit
Integer specifying a limit on the number of splits to be found.

Opis

Metoda split zwraca nową tablicę.

When found, separator is removed from the string and the substrings are returned in an array. If separator is omitted, the array contains one element consisting of the entire string.

Uwaga: W przypadku pustego ciągu znaków split nie zwraca pustej tablicy lecz tablicę zawierającą pusty ciąg znaków.

Kompatybilność wsteczna

JavaScript 1.2

W JavaScript 1.2 i późniejszych split posiada następujące rozszerzenia:

  • It can take a regular expression argument, as well as a fixed string, by which to split the object string. If separator is a regular expression, any included parenthesis cause submatches to be included in the returned array.
  • It can take a limit count so that the resulting array does not include trailing elements.
  • W JavaScript 1.2, string.split(" ") would split on any run of 1 or more white space characters including spaces, tabs, line feeds, and carriage returns.

Przykłady

Przykład: Zastosowanie split

The following example defines a function that splits a string into an array of strings using the specified separator. After splitting the string, the function displays messages indicating the original string (before the split), the separator used, the number of elements in the array, and the individual array elements.

function splitString (stringToSplit,separator) {
   arrayOfStrings = stringToSplit.split(separator)
   document.write ('<P>The original string is: "' + stringToSplit + '"')
   document.write ('<BR>The separator is: "' + separator + '"')
   document.write ("<BR>The array has " + arrayOfStrings.length + " elements: ")

   for (var i=0; i < arrayOfStrings.length; i++) {
      document.write (arrayOfStrings[i] + " / ")
   }
}

var tempestString="Oh brave new world that has such people in it."
var monthString="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

var space=" "
var comma=","

splitString(tempestString,space)
splitString(tempestString)
splitString(monthString,comma)

Ten przykład daje następujący wynik:

The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it. /

The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it. /

The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec /

Przykład: Różnice pomiedzy split w wersji JavaScript 1.2, a innymi wersjami

Consider the following script:

str="She sells seashells \nby the\n seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)

Using JavaScript 1.2, this script produces:

"She", "sells", "seashells", "by", "the", "seashore"

In other versions, this script splits only on single space characters, producing:

"She", "sells", "seashells", "\nby", "the\n", "seashore"

Przykład: Usuwanie spacji z łańcucha znaków

In the following example, split looks for 1 or more spaces followed by a semicolon followed by 1 or more spaces and, when found, removes the spaces from the string. nameList is the array returned as a result of split.

<SCRIPT>
names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write (names + "<BR>" + "<BR>");
re = /\s+;\s+/;
nameList = names.split (re);
document.write(nameList);
</SCRIPT>

This prints two lines; the first line prints the original string, and the second line prints the resulting array.

Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris Hand

Przykład: Returning a limited number of splits

In the following example, split looks for 0 or more spaces in a string and returns the first 3 splits that it finds.

myVar = " Hello World. How are you doing? ";
splits = myVar.split(" ", 3);
document.write(splits)

This script displays the following:

["Hello", "World.", "How"]

Zobacz także

charAt, indexOf, lastIndexOf

 

Autorzy i etykiety dokumentu

Autorzy tej strony: Ptak82, grzegorz, splewako, Diablownik, Mgjbot
Ostatnia aktualizacja: splewako,