forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Ruby String Methods
Quincy Larson edited this page Aug 20, 2016
·
1 revision
Ruby has many built in methods to work with strings. Strings in Ruby by default are mutable and can be changed in place or a new string can be returned from a method.
- The
.lengthproperty returns the number of characters in a string including white-space.
"Hello".length
# returns: 5"Hello World!".length
# returns: 12- The
.countmethod counts how many times a specific character(s) is found in a string. - This method is case-sensitive.
"HELLO".count('L')
# returns: 2"HELLO WORLD!".count('LO')
# returns: 1- The
.insertmethod inserts a string into another string before a given index.
"Hello".insert(3, "hi5")
# returns:
Helhi5lo
# "hi5" is inserted into the string right before the second 'l' which is at index 3- The
.upcasemethod transforms all letters in a string to uppercase.
"Hello".upcase
# returns:
HELLO- The
.downcasemethod transforms all letters in a string to lowercase.
"Hello".downcase
# returns:
hello- The
.capitalizemethod make the first letter in a string uppercase and the rest of the string lowercase.
"HELLO".capitalize
# returns:
Hello"HELLO, HOW ARE YOU?".capitalize
# returns:
Hello, how are you?Note that the first letter is only capitilized if it is at the beginning of the string.
"-HELLO".capitalize
"1HELLO".capitalize
# returns:
-hello
1hello- The
.reversemethod reverses the order of the characters in a string.
"Hello World!".reverse
# returns:
"!dlroW olleH"- The
.splittakes a strings and splits it into an array, then returns the array. - The default method splits the string based on whitespace, unless a different separator is provided (see second example).
"Hello, how are you?".split
# returns:
["Hello,", "how", "are", "you?"]"H-e-l-l-o".split('-')
# returns:
["H", "e", "l", "l", "o"]- The
.chopmethod removes the last character of the string. - A new string is returned, unless you use the
.chop!method which mutates the original string.
"Name".chop
# returns:
Namname = "Batman"
name.chop
name == "Batma" # returns falsename = "Batman"
name.chop!
name == "Batma" # returns true- The
.stripmethod removes the leading and trailing whitespace on strings, including tabs, newlines, and carriage returns (\t,\n,\r).
" Hello ".strip
# returns:
Hello- The
.chompmethod removes the last character in a string, only if it’s a carriage return or newline (\r,\n). - This method is commonly used with the
getscommand to remove returns from user input.
"hello\r".chomp
# returns:
hello"hello\t".chomp
# returns:
hello\t #because tabs and other whitespace remain intact when using `chomp`- The
.to_imethod converts a string to an integer.
"15".to_i
# returns:
15 #integer| Previous | Home |
|---|---|
| Ruby String Operators | Table of Contents |
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links