Built-in String and Array Methods
Learn to use popular built-in Methods related to strings and arrays in Ruby.
We'll cover the following...
Built-in methods for strings
Ruby provides various built-in Methods for dealing with strings. These methods are collectively called string methods.
Note: All string methods return a new string without changing the original one.
There are numerous tasks related to strings that make it easy for programmers to perform routine tasks. This section will provide examples and results of some commonly used string methods.
The methods related to case change
These methods change the letter case of the text stored in a string (say, from lowercase to uppercase). Let’s use an example program to explore case changes.
- The
capitalize()method converts the first character of the string to uppercase. - The
downcase()method converts all characters in the string to lowercase. - The
swapcase()method swaps the letter case of the characters in a string—downcase becomes the uppercase and vice versa. - The
upcase()method converts all characters in a string to uppercase.
The search-related methods
These methods search for the occurrence of text in a string. Let’s use an example program to explore searches.
- The
end_with?method checks for text at the end of a string and returnstrueorfalse. - The
indexmethod searches for text in the string and returns the index of the first occurrence. - The
include?method searches for text in the string and returnstrueorfalse. - The
countmethod searches for characters in the string and returns the number of occurrences. - The
gsubmethod searches for all occurrences of a text in the string, replaces it with different text, and returns the new string after replacement. - The
start_with?method checks for text at the start of a string and returnstrueorfalse. - The
empty?method checks if a string is empty or not.
Built-in methods for arrays
Ruby provides various built-in functions for dealing with arrays, usually termed array methods, which programmers use to perform routine tasks. Let’s use practice programs to learn these built-in methods related to arrays.
The value-inserting methods
The following methods are used to add values to arrays:
- The
append()method adds an element at the end of the array. If we try to append another array, it’s added as a single element, making a nested array. - The
clone()method creates a copy of an existing array. Future updates to the original array don’t affect the copy. This differs from the assignment operator=, which only creates a reference to an existing array. In the case of assignment, any change to the original array is reflected in the reference. This difference is illustrated in the code below. - The
push()method appends multiple elements to the end of the array. This method can append an array to another array, element by element. - The
insert()method adds an element at a specified position in the array.
The value-removing methods
The following methods are used to remove values from arrays:
- The
clear()method removes all the elements from the array. - The
pop()method removes the last element from the array. - The
delete()method removes the specified value from the array. - The
delete_at()method removes the value from the specified index.
The search-related methods
The following methods are used to search for values in arrays:
- The
count()method searches for an element in the array and returns the number of occurrences. - The
index()method searches for an element in the array and returns the index of the first occurrence.
The arrangement-related methods
The following methods are used to change the arrangement of values in arrays:
- The
reverse()method reverses the order of the array. - The
sort()method sorts the array.