Dokumentacja języka JavaScript 1.5:Obiekty:String:split
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
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...
Spis treści |
[edytuj] 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 (if separator is a string) ECMA-262, Edycja 3 (if separator is a regular expression) |
[edytuj] Składnia
split([separator][, limit])
[edytuj] Parametry
-
separator - Specifies the character to use for separating the string. The
separatoris treated as a string or a regular expression. Ifseparatoris 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.
[edytuj] 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: When the string is empty, split returns an array containing one empty string, rather than an empty array.
[edytuj] Kompatybilność wsteczna
[edytuj] JavaScript 1.2
In JavaScript 1.2 or later, split has the following additions:
- It can take a regular expression argument, as well as a fixed string, by which to split the object string. If
separatoris 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.
[edytuj] Przykłady
[edytuj] 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 /
[edytuj] 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"
[edytuj] 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
[edytuj] 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"]