Created
January 31, 2018 19:20
-
-
Save ilyakrasnov/d3bced30e6732eeadebc3e9a43e984e7 to your computer and use it in GitHub Desktop.
Different String methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Downcase, Upcase, Capitalize | |
name = "Peter" | |
name.upcase # => "PETER" | |
name.downcase # => "peter" | |
name = "pEtEr" | |
name.capitalize # => "Peter" | |
## Characters | |
name = "Peter" | |
name.chars # => ["P", "e", "t", "e", "r"] | |
## Split | |
url = "http://www.google.com?search=kittens" | |
url.split(".") # => ["http://www", "google", "com?search=kittens"] | |
## Converting to Integer and back to String | |
### Convert string to integer | |
age = "16" | |
age.class # => String | |
age.to_i.calss # => Integer | |
### Convert integer to string | |
age = 22 | |
age.class # => Integer | |
age.to_s.calss # => String | |
## Reverse | |
secret = "Hello, this is my secret message" | |
secret.reverse # => "egassem terces ym si siht ,olleH" | |
## Substitution | |
text = "I would like to invite you to my wedding" | |
text.sub("I", "We") # => "We would like to invite you to my wedding." | |
text.sub("I", "We").sub("my","our") # => "We would like to invite you to our wedding." | |
## Length of a string | |
phone = "34352353523" | |
phone.length # => 11 | |
## Greater and less than | |
"Bob" > "Alice" # => true | |
"A" > "a" # => false | |
## Index of | |
greeting = "Hello" | |
greeting.index("o") # => 4 | |
greeting.index("w") # => nil | |
## include? , start_with? and end_with? | |
abba = "Abba" | |
abba.include?("bb") # => true | |
abba.include?("bc") # => false | |
email = "peter.pan@gmail.com" | |
email.include?("@") # => true | |
email.include?("yahoo") # => false | |
phone = "+1555645645645" | |
phone.start_with?("+1") # => true | |
email.end_with?("gmail.com") # => true | |
email.end_with?("yahoo.com") # => false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment