String
has several methods that can be used to manipulate strings. Â
These methods are called on the string itself and are often chained together to perform multiple operations on a string.
The following is a list of some of the most common methods.
See the String
API documentation for a complete list of methods.
The upcase
method returns a new string with all the characters converted to uppercase.
"hello".upcase
# => "HELLO"
The downcase
method returns a new string with all the characters converted to lowercase.
"HELLO".downcase
# => "hello"
Several methods can do more specific case conversions. That can be useful when converting a string to a specific format. Here is a list of some of the most common methods:
Method | Description | Example |
---|---|---|
String#capitalize |
Converts the first letter to uppercase and the rest to lowercase | "hEllO" # => "Hello" |
String#camelcase |
Converts underscore to camelcase | "eiffel_tower" # => "EiffelTower" |
String#camelcase(lower: true) |
Same as String#camelcase but leaves the first character lowercased | "empire_state_building" # => "empireStateBuilding" |
String#underscore |
Converts all characters to downcase and places an underscore where between a downcase letter and an upcased letter | "PartyInTheUSA" # => "party_in_the_usa" |
String#titleize |
Capitalizes the first letter for each word and downcase the rest | "the great gatsBY" # => "The Great Gatsby" |
Crystal has methods for replacing parts of a string.
These are known as sub
(substitution) and gsub
(global substitution).
These methods can take a string, char, or regex as the first argument and a string or char as the second argument.
The first argument is the part of the string that will be replaced, and the second argument is the replacement.
Char
and regex will be explained in later concepts.
The sub
method will replace the first occurrence of the first argument with the second argument.
"hello".sub("l", "r")
# => "herlo"
The gsub
method works the same as sub
but will replace all occurrences of the first argument with the second argument.
"hello".gsub("l", "r")
# => "herro"
Chomp and strip can be used to remove unwanted characters from a string.
The chomp
method will be default remove the last \r
, \n
or \r\n
characters from a string.
If the method is called with an argument, it will remove the given value from the end of the string.
If it does exist at the end of the string.
"hello\n".chomp
# => "hello"
"hello".chomp("llo")
# => "he"
By default, the strip
method will remove all whitespace from the beginning and end of a string.
If the method is called with an argument, it will remove that argument from the beginning and end of the string.
If it does exist at the beginning or end of the string.
" hello ".strip
# => "hello"
You can use the reverse
method to reverse a string.
This method will return a new string with the characters in reverse order.
"hello".reverse
# => "olleh"
The index
method can be used to find a character's index in a string.
The index
method takes a String
, Char
, or regex as an argument.
It can take an optional second argument to specify the index from which to search.
The index
method will return the index of the first occurrence of the argument.
"hello".index("l")
# => 2
"hello".index("l", 3)
# => 3
You can use the delete_at
method to remove a character from a string.
The delete_at
method takes an index as an argument and will remove the character at that index.
"hello".delete_at(0)
# => "ello"
"hello".delete_at(3)
# => "helo"
The insert
method can be used to insert a string at a specific index.
The insert
method takes an index as the first argument and a string as the second argument.
The insert
method will return a new string with the second argument inserted at the index.
"hello".insert(0, "h")
# => "hhello"
"hello".insert(3, "l")
# => "helllo"