These are the method frequently used for strings.
1. []=
String can act like an array.
Eg:
"hello"[1] # return "e"
"hello"[-2] # return "l"
"hello"[1..-1] # return "ello"
2. chomp
It will remove any record separator like enter key when called.
Return the string with record separator removed.
3. to_i
Convert strings into integer. If non-numeric character is called, then it will return zero.
Eg:
"1".to_i # return 1
"s".to_i # return 0
4. capitalize , upcase, downcase
Capitalize convert first letter to capital. Upcase convert whole string to capital. Downcase convert whole string to lowercase.
Return the modified string.
5. chars
Split the string into individual character's array.
Eg:
"hello".chars # return ['h','e','l','l','o']
6. gsub
Replace characters with another character
Eg:
"hello".gsub("h", "e") # return "eello"
7. include?
Return true of string contains character, false otherwise.
Eg:
"hello".include?("e") # return true
8. length
Return the number of characters in a string.
Eg:
"hello".length # return 5
9. slice
Slice the string into individual characters.
Eg:
"hello".slice(0,2) # return "he"
"hello".slice(4) # return "o"
"hello".slice(2..4) # return "llo"
10. split
Split the string according to specified delimiter. Return array.
Eg:
"hello world".split(" ") # return ["hello", "world"]
NOTE: " " is removed.
"hello world".split("o") # return ["hell" , " w", "rld"]
NOTE: "o" is removed.
11. count
Return the number of time a character occured in a string.
Eg:
"hello".count("l") # return 2
12. << , prepend , chop
Eg:
"hello" << "e" # return "helloe"
"hello".prepend("e") # return "ehello"
"hello".chop # return " hell"
13. scan
Return an array of the argument.
Eg:
"hello".scan("l")
# return ['l', 'l']
14. squeeze
Return a string with consecutive character removed
Eg:
" e ".squeeze(" ")
# return " e "
1. []=
String can act like an array.
Eg:
"hello"[1] # return "e"
"hello"[-2] # return "l"
"hello"[1..-1] # return "ello"
2. chomp
It will remove any record separator like enter key when called.
Return the string with record separator removed.
3. to_i
Convert strings into integer. If non-numeric character is called, then it will return zero.
Eg:
"1".to_i # return 1
"s".to_i # return 0
4. capitalize , upcase, downcase
Capitalize convert first letter to capital. Upcase convert whole string to capital. Downcase convert whole string to lowercase.
Return the modified string.
5. chars
Split the string into individual character's array.
Eg:
"hello".chars # return ['h','e','l','l','o']
6. gsub
Replace characters with another character
Eg:
"hello".gsub("h", "e") # return "eello"
7. include?
Return true of string contains character, false otherwise.
Eg:
"hello".include?("e") # return true
8. length
Return the number of characters in a string.
Eg:
"hello".length # return 5
9. slice
Slice the string into individual characters.
Eg:
"hello".slice(0,2) # return "he"
"hello".slice(4) # return "o"
"hello".slice(2..4) # return "llo"
10. split
Split the string according to specified delimiter. Return array.
Eg:
"hello world".split(" ") # return ["hello", "world"]
NOTE: " " is removed.
"hello world".split("o") # return ["hell" , " w", "rld"]
NOTE: "o" is removed.
11. count
Return the number of time a character occured in a string.
Eg:
"hello".count("l") # return 2
12. << , prepend , chop
Eg:
"hello" << "e" # return "helloe"
"hello".prepend("e") # return "ehello"
"hello".chop # return " hell"
13. scan
Return an array of the argument.
Eg:
"hello".scan("l")
# return ['l', 'l']
14. squeeze
Return a string with consecutive character removed
Eg:
" e ".squeeze(" ")
# return " e "
Comments
Post a Comment