String and Array Operations
A string of characters can be thought of a special type of array (an array of characters). In fact, an array of characters is used to implement the string data type in lower-level languages, like C. Therefore, many operations and functions that manipulate arrays can also be used with strings. Let's look at some of them.
String Length
Just like with an array, use the len() function to find the number of characters in a string:
console
Accessing Characters
Again, like you would with an array, you can reach for individual characters in a string using a zero-based index:
console
Unlike arrays, you cannot modify a string in this way:
console
Strings in Yas are immutable - meaning you cannot directly modify their contents.
Concatenation
We've been using the + operator to concatenate (paste) strings together. We can also concatenate arrays in the same way:
console
Slicing
To slice a string into substrings, use the slice() built-in function:
slice(str, start, end)will return the substring of string str starting at index start and up to, but not including, the index end.
console
The end parameter can be omitted, in which case the returned substring will stretch to the end of the string:
console
Both start and end parameters can be negative, in which case the counting of characters starts from the end of the string. For example, -2 refers to the second to last character in the string.
print slice("Hello, Bob", -3) # BobThe slice() function can also be used to slice arrays:
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
let weekdays = slice(days, 1, 6)
print weekdays
let weekends = slice(days, 0, 1) + slice(days, 6)
print weekendsSplicing
Suppose you wanted to edit a string: replace some characters in the string with some other string (possibly empty). We know that strings cannot be modified directly, but we can use a combination of slicing and concatenation to make an edited string from the original:
console
The result is that characters in the original string between indices 5 and 8 were replaced by another string.
There is a shortcut for this situation: the splice() function. Its general form looks like this:
splice(original, start, end, replacement)The start and end parameter have exactly the same semantics as the slice() function above. (Inclusive of start and exclusive of end; negative values can be specified).
For example:
console
If you provide an empty replacement string, or omit this argument altogether, then the slice is simply deleted:
console
If start and end are equal to each other, then replacement will be inserted at the start position, without changing any of the other characters:
console
Similarly, the splice() function can be applied to arrays:
console
Searching
Use the builtin indexOf() function to search for elements in arrays and characters in strings. It returns the first index at which the element or character is found, or -1 if a match cannot be located.
With arrays:
console
With strings:
console
By providing an additional start parameter, you can start searching from another index instead:
console
Pushing and Popping
You can append an element to the end of an array using push() and you can remove the last element from the array using pop():
console
Note that both push() and pop() alter the length of the array - either by growing or shrinking it. Neither of these functions can be used with strings.
Reversing
Reverse an array or a string with the reverse() function:
console
Sorting
Finally, you can sort an array in ascending order using the sort() function:
console
Note that the sort() function can only sort an array of numbers or an array of strings.