To get a substring after a specific character in Ruby, you can use the split
method of the String class and access the substring after the desired character.
In this example,
str
with a value of "key:value"
.split
method on str
with the character ':'
as the delimiter. The split
method divides the string into an array of substrings based on the specified delimiter.str = "key:value"
substring = str.split(":")[1]
puts "Substring after ':' is: #{substring}"
Substring after ':' is: value
In this example,
str
with a value of "Hello,World"
.split
method on str
with the character ','
as the delimiter. The split
method divides the string into an array of substrings based on the specified delimiter.str = "Hello,World"
substring = str.split(",")[1]
puts "Substring after ',' is: #{substring}"
Substring after ',' is: World
In this example,
str
with a value of "First Second"
.split
method on str
with the space character ' '
as the delimiter. The split
method divides the string into an array of substrings based on the specified delimiter.str = "First Second"
substring = str.split(" ")[1]
puts "Substring after ' ' is: #{substring}"
Substring after ' ' is: Second
In this tutorial, we learned How to get Substring after Specific Character in Ruby language with well detailed examples.