Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 635 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check whether a string contains a substring in Ruby

#1
I have a string variable with content:

varMessage =
"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n"


"/my/name/is/balaji.so\n"
"call::myFunction(int const&)\n"
"void::secondFunction(char const&)\n"
.
.
.
"this/is/last/line/liobrary.so"

In the string I have to find a sub-string:

"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n"

"/my/name/is/balaji.so\n"
"call::myFunction(int const&)\n"

How can I find it? I need to determine whether the sub-string is present or not.
Reply

#2
user_input = gets.chomp
user_input.downcase!

if user_input.include?('substring')
# Do something
end

This will help you check if the string contains substring or not



puts "Enter a string"
user_input = gets.chomp # Ex: Tommy
user_input.downcase! # tommy


if user_input.include?('s')
puts "Found"
else
puts "Not found"
end

Reply

#3
You can use the [String Element Reference][1] method which is `[]`

Inside the `[]` can either be a literal substring, an index, or a regex:

> s='abcdefg'
=> "abcdefg"
> s['a']
=> "a"
> s['z']
=> nil

Since `nil` is functionally the same as `false` and any substring returned from `[]` is `true` you can use the logic as if you use the method `.include?`:

0> if s[sub_s]
1> puts "\"#{s}\" has \"#{sub_s}\""
1> else
1* puts "\"#{s}\" does not have \"#{sub_s}\""
1> end
"abcdefg" has "abc"

0> if s[sub_s]
1> puts "\"#{s}\" has \"#{sub_s}\""
1> else
1* puts "\"#{s}\" does not have \"#{sub_s}\""
1> end
"abcdefg" does not have "xyz"

Just make sure you don't confuse an index with a sub string:

> '123456790'[8] # integer is eighth element, or '0'
=> "0" # would test as 'true' in Ruby
> '123456790'['8']
=> nil # correct

You can also use a regex:

> s[/A/i]
=> "a"
> s[/A/]
=> nil






[1]:

[To see links please register here]

Reply

#4
Ternary way

my_string.include?('ahr') ? (puts 'String includes ahr') : (puts 'String does not include ahr')

OR

puts (my_string.include?('ahr') ? 'String includes ahr' : 'String not includes ahr')
Reply

#5
> How to check whether a string contains a substring in Ruby?

When you say 'check', I assume you want a boolean returned in which case you may use [`String#match?`][1]. `match?` accepts strings or regexes as its first parameter, if it's the former then it's automatically converted to a regex. So your use case would be:

str = 'string'
str.match? 'strings' #=> false
str.match? 'string' #=> true
str.match? 'strin' #=> true
str.match? 'trin' #=> true
str.match? 'tri' #=> true

`String#match?` has the added benefit of an optional second argument which specifies an index from which to search the string. By default this is set to `0`.

str.match? 'tri',0 #=> true
str.match? 'tri',1 #=> true
str.match? 'tri',2 #=> false

[1]:

[To see links please register here]

Reply

#6
You can use the [`include?`][1] method:

my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
end


[1]:

[To see links please register here]

Reply

#7
Expanding on Clint Pachl's answer:

Regex matching in Ruby returns `nil` when the expression doesn't match. When it does, it returns the index of the character where the match happens. For example:

"foobar" =~ /bar/ # returns 3
"foobar" =~ /foo/ # returns 0
"foobar" =~ /zzz/ # returns nil

It's important to note that in Ruby only `nil` and the boolean expression `false` evaluate to false. Everything else, including an empty Array, empty Hash, or the Integer 0, evaluates to true.

That's why the `/foo/` example above works, and why.

if "string" =~ /regex/

works as expected, only entering the 'true' part of the `if` block if a match occurred.

Reply

#8
A more succinct idiom than the accepted answer above that's available in Rails (from 3.1.0 and above) is `.in?`:



my_string = "abcdefg"
if "cde".in? my_string
puts "'cde' is in the String."
puts "i.e. String includes 'cde'"
end

I also think it's more readable.

See the [`in?`][1] documentation for more information.

Note again that it's only available in *Rails*, and not pure Ruby.


[1]:

[To see links please register here]

Reply

#9
You can also do this...

my_string = "Hello world"

if my_string["Hello"]
puts 'It has "Hello"'
else
puts 'No "Hello" found'
end

# => 'It has "Hello"'

This example uses Ruby's String [`#[]`](

[To see links please register here]

) method.
Reply

#10
In case you can not use one of the libs mentioned above, one could achieve the same with simple text search (this is ignoring cases because of `downcase`):

ADD_BUTTON_TEXTS = ["add to cart", "add to basket"].freeze
target_text = "AdD tO cArT"
ADD_BUTTON_TEXTS.each do |text|
puts "Text was found" if target_text.downcase.include?(text)
end
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through