Java Reference
In-Depth Information
Beef",
"Chicken and Mushroom"]
Now the pizzas array contains these two new strings.
The join() Method
The join() method can be used to turn the array into a string that comprises all the items
in the array, separated by commas. This is as if Donatello was asked to write down a list of
all the pizzas in his pile of boxes:
pizzas.join();
<< "Chicken & Bacon,Mushroom,Pepperoni,Spicy Beef,Chicken
and Mushroom"
You can choose a separator other than a comma by placing it inside the parentheses. Let's
try using an ampersand:
pizzas.join(" & ");
<< "Chicken & Bacon & Mushroom & Pepperoni & Spicy Beef &
Chicken
and Mushroom"
Slicing and Splicing
Be careful not to get confused with the pizza analogy here―we're slicing the array, not the
actual pizzas! The slice() method creates a subarray, effectively chopping out a slice of
an original array starting at one index and finishing at the next. This would be the same as
Donatello taking a selection of the pizza boxes from the whole pile, from the second pizza
up to but not including the fourth:
pizzas.slice(2,4) // starts at the third item (index of 2)
and
finishes at the fourth (the item with index 4 is not
included)
<< ["Pepperoni", "Spicy Beef"]
Search WWH ::




Custom Search