Beruflich Dokumente
Kultur Dokumente
Strings are objects so they have a lot of methods you can use to d
In this article you’ll discover the most useful Ruby string methods
https://www.rubyguides.com/2018/01/ruby-string-methods/ 1/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
"ruby".size
# 4
name = "Jesus"
What some people don’t know is that you can have actual code ins
https://www.rubyguides.com/2018/01/ruby-string-methods/ 2/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
Here’s an example:
Ruby calls the to_s method on the string interpolation block, this
string.
Like this:
string[0,3]
# abc
string[3,3]
# 123
The first number is the starting index & the second number is how
You can also use a range if you want to do something like “get all t
Example:
string = "abc123"
https://www.rubyguides.com/2018/01/ruby-string-methods/ 3/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
string[0..-2]
# "abc12"
The first index is the starting index & the second index is the endin
second to last character, and -1 is the end of the string.
string[0..2] = ""
p string
# "123"
Another String
What’s the easiest way to find if a string is included in another strin
string.include?("Saturday")
# true
https://www.rubyguides.com/2018/01/ruby-string-methods/ 4/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
string.index("day")
# 2
This method looks for partial words & instead of returning true or
the start of this string is found.
If you want to find patterns (like all the words containing the word
expressions.
# "00001101"
binary_string = "1111"
binary_string.ljust(8, "0")
# "11110000"
Example:
lang1 = "ruby"
lang2 = "Ruby"
lang1.upcase == lang2.upcase
Space
When reading data from a file or a website you may find yourself w
You can remove that extra space with the strip method:
# "test"
If you only want to remove the white space from one of the sides (
rstrip methods instead.
https://www.rubyguides.com/2018/01/ruby-string-methods/ 6/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
Here’s an example:
string.start_with? "ruby"
# true
string.end_with? "programming"
# true
Here’s an example:
string.delete_suffix(" is expensive")
# "bacon"
Example:
string.split
# ["a", "b", "c", "d"]
csv = "a,b,c,d"
string.split(",")
# ["a", "b", "c", "d"]
Share this post! = Share q Tweet - Share
But if you are working with CSV data specifically you may want to c
standard library. This class can do things like reading column head
Example:
https://www.rubyguides.com/2018/01/ruby-string-methods/ 8/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
arr.join
# "abc"
Example:
arr.join("-")
# "a-b-c"
Example:
Share this post! = Share q Tweet - Share
"49".to_i
Notice that if you try this with a string that contains no numbers th
Example:
"a".to_i
# 0
"123".match?(/\A-?\d+\Z/)
# true
"123bb".match?(/\A-?\d+\Z/)
# false
string = ""
https://www.rubyguides.com/2018/01/ruby-string-methods/ 10/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
# "hello there"
You can also use the chars method to convert the string into an
each on this array to iterate.
Share this post! = Share q Tweet - Share
Example:
array_of_characters = "rubyguides".chars
# ["r", "u", "b", "y", "g", "u", "i", "d", "e", "s"]
Example:
https://www.rubyguides.com/2018/01/ruby-string-methods/ 11/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
"abcd".upcase
# "ABCD"
And if you want to convert to lower case you can use the downca
Example:
"ABCD".downcase
# "abcd"
b = <<-STRING
aaa
bbb
ccc
STRING
a = %Q(aaa
bbb
ccc
)
https://www.rubyguides.com/2018/01/ruby-string-methods/ 12/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
string.gsub("dogs", "cats")
If you want to remove the string use an empty string as the 2nd ar
Example:
string.gsub("c", "")
# "ab"
Now:
If you want to apply the changes to the original string you can use
Here's an example:
string.gsub(/\d+/, "5")
This replaces all the numbers ( \d+ ) in the string with the number
https://www.rubyguides.com/2018/01/ruby-string-methods/ 14/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
Well, sub is the same as gsub , but it will only replace the first ma
Example:
# type something...
Example:
name = gets.chomp
Since Ruby 2.3 the chomp method takes an optional argument tha
you want to remove.
https://www.rubyguides.com/2018/01/ruby-string-methods/ 15/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
Example:
"abcd?".chomp("?")
# "abcd"
And if the character is not there it will return the original string.
But there are also more complex encodings, like UTF-8, which allo
different languages (Chinese, etc.) & even emojis.
Share this post! = Share q Tweet - Share
To find out the current encoding for a string you can use the enco
"abc".encoding
# Encoding:UTF-8
Like this:
https://www.rubyguides.com/2018/01/ruby-string-methods/ 16/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
"abc".force_encoding("UTF-8")
Counting Characters
You can count how many times a characters appears in a string by
Example:
str = "aaab"
str.count("a")
# 3
str.count("b")
# 1
You learned about many string methods, like join & split to break d
replace text inside strings & strip to trim out extra white space.
Since you may want to reference this page later make sure to book
friends
https://www.rubyguides.com/2018/01/ruby-string-methods/ 17/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
Related
Tweet Share
17 comments
Maddox says
last year
https://www.rubyguides.com/2018/01/ruby-string-methods/ 18/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
David C says
last year
Abdullah H says
last year
https://www.rubyguides.com/2018/01/ruby-string-methods/ 19/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
last year
str.casecmp('ABC').zero?
Tara says
last year
Simon says
last year
https://www.rubyguides.com/2018/01/ruby-string-methods/ 21/23
2/6/2019 Ruby String Methods (Ultimate Guide) - RubyGuides
last year
https://www.rubyguides.com/2018/01/ruby-string-methods/ 23/23