To find the characters in the first string that are not present in the second string in Ruby, you can use the delete
method to remove characters that appear in the second string from the first string.
In this example,
str1
with a string value of "Hello World"
.str2
with a string value of "lorem ipsum"
.delete
method of the string object str1
to remove any character that appears in str2
. The method delete
is called on str1
and passed str2
as an argument.str2
removed from str1
.str1 = "Hello World"
str2 = "lorem ipsum"
result = str1.delete(str2)
puts "Characters in '#{str1}' not in '#{str2}' are: #{result}"
Characters in 'Hello World' not in 'lorem ipsum' are: HWd
In this example,
str1
with a string value of "abcdef"
.str2
with a string value of "defghi"
.delete
method of the string object str1
to remove any character that appears in str2
. The method delete
is called on str1
and passed str2
as an argument.str2
removed from str1
.str1 = "abcdef"
str2 = "defghi"
result = str1.delete(str2)
puts "Characters in '#{str1}' not in '#{str2}' are: #{result}"
Characters in 'abcdef' not in 'defghi' are: abc
In this example,
str1
with a string value of "1234567890"
.str2
with a string value of "2468"
.delete
method of the string object str1
to remove any character that appears in str2
. The method delete
is called on str1
and passed str2
as an argument.str2
removed from str1
.str1 = "1234567890"
str2 = "2468"
result = str1.delete(str2)
puts "Characters in '#{str1}' not in '#{str2}' are: #{result}"
Characters in '1234567890' not in '2468' are: 135790
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Ruby language with well detailed examples.