A.4 String
A.4.1 .concat (.+)
String#concat(Integer) ⇒ String
or
String#concat(String) ⇒ String
Appends the given arguments to a string. when given an integer as an argument, it converts the integer into ASCII code.
"hi".concat(33) # => "hi!"
"Rub".concat(121) # => "Ruby"
When given a string literal as an argument, it adds that string to the original string. .+
or +
is similar to the .concat
method. +
will return a new combined String
instead of modifying the original String
. Each line of code below will give the same output.
"hi".concat(" there")
"hi".+(" there")
"hi" +(" there")
"hi" + " there"
This method returns a new String
A.4.2 * method
Multiplies the original string by the given integer and returns the new modified String
.
"Ya" * 5
Returns "YaYaYaYaYa"
"Ya" * 0
Returns ""
A.4.3 .upcase
String#upcase ⇒ String
Converts all lowercase letters to their uppercase counterparts in the given string and returns the new modified String
.
"hello".upcase`
Returns
"HELLO"
A.4.4 .downcase
String#downcase ⇒ String
Converts all the uppercase letters to their lowercase counterparts from the given String
. Returns the new modified String
.
"HI".downcase`
Returns
"hi"
A.4.5 .swapcase
String#swapcase ⇒ String
Converts all the uppercase letters to their lowercase counterparts and lowercase letters to their uppercase counterparts from the given string. Returns the new modified String
.
"Hi There".swapcase
Returns
"hI tHERE"
"hI tHERE".swapcase
Returns
"Hi There"
A.4.6 .reverse (String)
String#reverse ⇒ String
Returns a new String
with the characters from the original String in reverse order.
"stressed".reverse
Returns
"desserts"
A.4.7 .length
String#length ⇒ Integer
Returns the Integer
number of charactersin the String
.
"hippopotamus".length
Returns
12
A.4.8 .chomp
String#chomp ⇒ String
or
String#chomp(String) ⇒ String
When not given any argument, removes the "\n"
(newline) character from the end of the string. When given an argument of a charcter or a string, it remove that argument from the end of the orginal string.
Returns a new String
with the character removed.
"Hey!\n".chomp
"Hey!".chomp("!")
"Hey There".chomp("There")
"Hey!".chomp("y")
"Hey!"
"Hey"
"Hey "
"Hey!"
"What is your name?"
p # supposes the user inputs "Clark" and then hits return
name = gets "Hi" + name p
Prints
"Hi Clark\n"
"What is your name?"
p # supposes the user inputs "Clark" and then hits return
name = gets "Hi" + name.chomp p
Prints
"Hi Clark"
"What is your name?"
p # supposes the user inputs "Clark" and then hits return
name = gets.chomp "Hi" + name p
"Hi Clark"
A.4.9 .gsub
String#gsub(String, String) ⇒ String
Substitutes the all occurances of the first argument with the second argument in original string and returns a new String
with the substitutes made.
"Hello".gsub("ello", "i")
Returns
"Hi"
"Hi.there".gsub(".", " ")
Returns
"Hi there"
Giving an empty string as the second arugment deletes any occurences of the first arugument in the string.
"example @ ruby.com".gsub(" ", "")
Returns
"example@ruby.com"
A.4.10 .to_i (String)
String#to_i ⇒ Integer
Converts a string literal that contains a number to an integer. The Integer
is returned.
"8".to_i
Returns
8
"What is your lucky number?"
p # Suppose the user types is "7" and then hits return
lucky_number = gets.chomp 2 # This will throw an error. square = lucky_number **
Returns
NoMethodError (undefined method '**' for "7":String)
This is where the .to_s
method comes handy.
"What is your lucky number?"
p # Suppose the user types is "7" and then hits return
lucky_number = gets.chomp 2 # This will throw an error.
square = lucky_numbe.to_i ** p square
Returns
"49"
A.4.11 .strip
String#strip ⇒ String
Removes all leading and trailing whitespace in the string.
Returns a new String
that has been modified from the original.
" hi there ".strip
Returns
"hi there"
A.4.12 .capitalize
String#capitalize ⇒ String
Capitalizes the first character of a string.
Returns a new String
that has been modified from the original.
"capitalize".capitalize
Returns
"Capitalize"
A.4.13 .split
String#split ⇒ Array
or
String#split(String) ⇒ Array
Splits a string into an substrings and creates an Array of these substrings. when not given argument, .split
uses whitespace to divide the string. when given an argument, .split
divides the string on that argument.
Returns an Array
of the divided String
.
"Hello hi byebye".split
Returns
["Hello", "hi", "byebye"]
"one!two!three!".split("!")
Returns
["one", "two", "three"]
A.4.14 .include?
String#include?(String) ⇒ Boolean
Returns a boolean(true
or false
) based on whether the string argument is inside the string the method is being called on.
"Happy".include?("H")
Returns
true
"Happy".include?("Z")
Returns
false