1. length or size: returns the length of the string.
str = "hello"
puts str.length
# Output: 5
puts str.size
# Output: 5
.jpg)
2. strip: returns a copy of the string with leading and trailing whitespace removed.
str = " hello "
puts str.strip
# Output: "hello"
3. upcase and downcase: return copies of the string with all characters in upper or lower case, respectively.
str = "Hello, World!"
puts str.upcase
# Output: "HELLO, WORLD!"
puts str.downcase
# Output: "hello, world!"
4. reverse: returns a copy of the string with the characters reversed.
str = "hello"
puts str.reverse
# Output: "olleh"
5. gsub: returns a copy of the string with all occurrences of a specified substring replaced with another string.
str = "hello, world!"
puts str.gsub("o", "0")
# Output: "hell0, w0rld!"
6. split: returns an array of substrings separated by a specified delimiter.
str = "hello,world"
puts str.split(",")
# Output: ["hello", "world"]
7. include?: returns true if the string contains a specified substring.
str = "hello, world!"
puts str.include?("world")
# Output: true
8. start_with? and end_with?: return true if the string starts or ends with a specified substring, respectively.
str = "hello, world!"
puts str.start_with?("hello")
# Output: true
puts str.end_with?("!")
# Output: true
9. slice: returns a substring specified by a range or index.
str = "hello, world!"
puts str.slice(0..4)
# Output: "hello"
puts str.slice(7)
# Output: "w"
10. concat or +: concatenates one or more strings to the end of the original string.
str1 = "hello"
str2 = "world"
puts str1.concat(" ", str2)
# Output: "hello world"
puts str1 + "!"
# Output: "hello world!"
11. insert: inserts a specified string at a specified index.
str = "hello, world!"
puts str.insert(5, " there")
# Output: "hello there, world!"
12. chomp: returns a copy of the string with any trailing newline characters removed.
str = "hello\n"
puts str.chomp
# Output: "hello"
13. gsub!: performs the same action as gsub, but modifies the original string instead of returning a copy.
str = "hello, world!"
str.gsub!("o", "0")
puts str
# Output: "hell0, w0rld!"
14. to_i and to_f: convert the string to an integer or float, respectively.
str = "123"
puts str.to_i + 1
# Output: 124
str = "3.14"
puts str.to_f + 1
# Output: 4.14
15. empty?: returns true if the string is empty.
str = ""
puts str.empty?
# Output: true
str = "hello"
puts str.empty?
# Output: false
16. index or rindex: returns the index of the first occurrence of a specified substring, or returns nil if the substring is not found. rindex works similarly, but searches from the end of the string.
str = "hello, world!"
puts str.index("o")
# Output: 4
puts str.rindex("o")
# Output: 8
puts str.index("z")
# Output: nil
17. gsub! with a block: performs the same action as gsub!, but allows for more complex replacements using a block.
str = "hello, world!"
str.gsub!("o") { |match| match.upcase }
puts str
# Output: "hellO, wOrld!"
18. strip!, lstrip!, and rstrip!: perform the same actions as strip, lstrip, and rstrip, but modify the original string instead of returning a copy.
str = " hello "
str.strip!
puts str
# Output: "hello"
19. center: returns a copy of the string centered within a specified width, with any extra space padded with a specified character.
str = "hello"
puts str.center(10, "-")
# Output: "--hello---"
20. chop: returns a copy of the string with the last character removed.
str = "hello"
puts str.chop
# Output: "hell"
21. split: splits the string into an array of substrings using a specified delimiter.
str = "hello,world,!"
arr = str.split(",")
puts arr.inspect
# Output: ["hello", "world", "!"]
22. swapcase: returns a copy of the string with uppercase letters converted to lowercase and vice versa.
str = "Hello, World!"
puts str.swapcase
# Output: "hELLO, wORLD!"
23. reverse: returns a copy of the string with the characters in reverse order.
str = "hello, world!"
puts str.reverse
# Output: "!dlrow ,olleh"
24. slice or [ ]: returns a substring of the original string based on a specified range of indices or a regular expression.
str = "hello, world!"
puts str.slice(2..6)
# Output: "llo, "
puts str[2..6]
# Output: "llo, "
puts str[/\w+/]
# Output: "hello"
25. prepend: adds a specified string to the beginning of the original string.
str = "world!"
str.prepend("hello, ")
puts str
# Output: "hello, world!"
26. concat: adds a specified string to the end of the original string.
str = "hello, "
str.concat("world!")
puts str
# Output: "hello, world!"
27. chomp and chomp!: removes the newline character from the end of a string. chomp! modifies the original string.
str = "hello\n"
str.chomp!
puts str
# Output: "hello"
28. bytes: returns an array of the numeric bytes that make up the string.
str = "hello"
arr = str.bytes
puts arr.inspect
# Output: [104, 101, 108, 108, 111]
29. length or size: returns the number of characters in the string.
str = "hello, world!"
puts str.length
# Output: 13
puts str.size
# Output: 13
30. include? or include: returns true if a specified substring is present in the string, otherwise returns false.
str = "hello, world!"
puts str.include?("world")
# Output: true
puts str.include?("z")
# Output: false
31. count: returns the number of occurrences of a specified character or substring in the string.
str = "hello, world!"
puts str.count("o")
# Output: 2
puts str.count("lo")
# Output: 5
32. delete or delete!: removes all occurrences of a specified character or substring from the string. delete! modifies the original string.
str = "hello, world!"
str.delete!("l")
puts str
# Output: "heo, word!"
33. tr or tr!: replaces all occurrences of a specified set of characters with a corresponding set of characters. tr! modifies the original string.
str = "hello, world!"
str.tr!("aeiou", "*")
puts str
# Output: "h*ll*, w*rld!"
34. insert: inserts a specified string at a specified index in the original string.
str = "hello, world!"
str.insert(5, " there")
puts str
# Output: "hello there, world!"
35. center: returns a new string padded with a specified character to center the original string.
str = "hello"
puts str.center(20, "*")
# Output: "*******hello*******"
36. ljust and rjust: returns a new string padded with a specified character to the left or right, respectively, to make the length of the new string equal to a specified length.
str = "hello"
puts str.ljust(10, "*")
# Output: "hello*****"
puts str.rjust(10, "*")
# Output: "*****hello"
37. strip, lstrip, and rstrip: remove leading and trailing whitespace characters from the string. strip!, lstrip!, and rstrip! modify the original string.
str = " hello, world! "
puts str.strip
# Output: "hello, world!"
puts str.lstrip
# Output: "hello, world! "
puts str.rstrip
# Output: " hello, world!"
str.strip!
puts str
# Output: "hello, world!"
38. gsub or gsub!: replaces all occurrences of a specified regular expression or string with a replacement string. gsub! modifies the original string.
str = "hello, world!"
str.gsub!(/[aeiou]/, "*")
puts str
# Output: "h*ll*, w*rld!"
39. slice or [ ]: returns a substring of the original string specified by a range of indices or a regular expression. Negative indices count from the end of the string. The [] syntax is equivalent to slice.
str = "hello, world!"
puts str.slice(0..4)
# Output: "hello"
puts str.slice(-6..-1)
# Output: "world!"
puts str.slice(/\w+,\s/)
# Output: "hello, "
40. split: returns an array of substrings obtained by splitting the original string at occurrences of a specified separator. If no separator is specified, whitespace is used by default.
str = "hello, world!"
puts str.split
# Output: ["hello,", "world!"]
puts str.split(",")
# Output: ["hello", " world!"]
41. scan: returns an array of all occurrences of a specified regular expression in the original string.
str = "hello, world!"
puts str.scan(/\w+/)
# Output: ["hello", "world"]
42. start_with?: returns true if the original string starts with a specified prefix, otherwise returns false.
str = "hello, world!"
puts str.start_with?("hello")
# Output: true
puts str.start_with?("world")
# Output: false
43. end_with?: returns true if the original string ends with a specified suffix, otherwise returns false.
str = "hello, world!"
puts str.end_with?("world!")
# Output: true
puts str.end_with?("hello")
# Output: false
44. concat or +: returns a new string obtained by concatenating the original string with one or more other strings. The + operator is equivalent to concat.
str = "hello"
puts str.concat(", world!")
# Output: "hello, world!"
str = "hello"
puts str + ", world!"
# Output: "hello, world!"
45. prepend: returns a new string obtained by concatenating one or more strings to the beginning of the original string.
str = "world!"
puts str.prepend("hello, ")
# Output: "hello, world!"

0 Comments