Check if char in string?

#1

how do i do this:

str = “you muppet”

if “y” in str:
print “its in there”

whats the best source for documentation on Ruby? i googled and checked
the homepage of ruby but there isnt really a clear one or im jus
duuumb.

#2

globalrev wrote:

how do i do this:

str = “you muppet”

if “y” in str:
print “its in there”

whats the best source for documentation on Ruby? i googled and checked
the homepage of ruby but there isnt really a clear one or im jus
duuumb.

str = “you muppet”
puts “its in there” if str.include? ‘y’

#3

On Wednesday 07 May 2008, globalrev wrote:

whats the best source for documentation on Ruby? i googled and checked
the homepage of ruby but there isnt really a clear one or im jus
duuumb.

http://www.ruby-doc.org/docs/ProgrammingRuby/

http://www.ruby-doc.org/core (reference for the core classes)

http://www.ruby-doc.org/stdlib/ (reference for the standard library)

Stefano

#4

you can use also Regular Expression:

!(“ciao” =~ /i/).nil?
#=> true

#5

On 7 Maj, 18:32, Marc H. removed_email_address@domain.invalid wrote:

duuumb.

str = “you muppet”
puts “its in there” if str.include? ‘y’


Posted viahttp://www.ruby-forum.com/.

hmm it only works for one character though. i want to do:
if x in ‘aeiouy’:
do this
else:
do that

#6

On 7 Maj, 19:39, removed_email_address@domain.invalid (matt neuburg) wrote:

if str =~ /y/

m.


matt neuburg, phd = removed_email_address@domain.invalid,http://www.tidbits.com/matt/
Leopard -http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript -http://www.amazon.com/gp/product/0596102119
Read TidBITS! It’s free and smart.http://www.tidbits.com

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

#7

globalrev removed_email_address@domain.invalid wrote:

how do i do this:

str = “you muppet”

if “y” in str:
print “its in there”

I always use regex in this situation:

if str =~ /y/

m.

#8

The expression he gave uses the =~ operator, not the == operator. It
will
work if you try it in IRB (which I suggest you do before you post next
time)…

Dan

#9

globalrev wrote:

On 7 Maj, 19:39, removed_email_address@domain.invalid (matt neuburg) wrote:

if str =~ /y/

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

Try the suggestion, you may be surprised :slight_smile:

#10

globalrev removed_email_address@domain.invalid wrote:

if str =~ /y/

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

Try it!

m.

#11

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

ok, you don’t know regexp

do it

#12

On 7 Maj, 22:04, RedWiz removed_email_address@domain.invalid wrote:

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

ok, you don’t know regexp

do it

but
if str =~ /aeiouy/
do foo
end

doesnt work. or i mena it works but iw ant to chekc if it is a vowel
not if contains the expr aeiouy.
how do i delimit the expressins?

#13

On Wed, May 7, 2008 at 1:40 PM, globalrev removed_email_address@domain.invalid wrote:

I always use regex in this situation:
Read TidBITS! It’s free and smart.http://www.tidbits.com

but none of that is what i want to do.

i want to chekc if a char is in a string.
not check if a char equals another char.

Along with the other suggestions, it might help you to read up on
regular expressions and what Ruby considers as true for a condition.

3 == false # false
3 == true # false

if 3; puts ‘hi’; end # hi
if {}; puts ‘hi’; end # hi
if []; puts ‘hi’; end # hi

Todd

#14

globalrev wrote:

if str =~ /aeiouy/
do foo
end

doesnt work. or i mena it works but iw ant to chekc if it is a vowel
not if contains the expr aeiouy.
how do i delimit the expressins?

str =~ /[aeiouy]/

This checks if str contains one of those chars. Regular expressions are
worth learning more about.

#15

globalrev wrote:

if str =~ /aeiouy/
do foo
end

doesnt work. or i mena it works but iw ant to chekc if it is a vowel
not if contains the expr aeiouy.
how do i delimit the expressins?

if str =~ /[aeiouy]/
do foo
end

[aeiouy] is a “character class”. The regular expression tests if any
character in the string is in the class.

#16

On Wed, May 7, 2008 at 4:48 PM, Tim H. removed_email_address@domain.invalid wrote:

not if contains the expr aeiouy.
how do i delimit the expressins?

if str =~ /[aeiouy]/
do foo
end

[aeiouy] is a “character class”. The regular expression tests if any
character in the string is in the class.

Get a good book on regular expressions. Trust me, it will seriously
help you in the long run for most any type of programming/system
interaction. If you want a quick look at some syntax,
http://www.regular-expressions.info might help. Note that, this is
only one of many ways to do what you are trying to do, but I think it
is the best way for this case.

hth,
Todd

#17

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

but i want to see if its not…
if str[x].chr !=~ /[aeiouy]/

doesnt work though…

and why cant i print [ in the windows ruby prompt?

#18

Hi,

globalrev wrote:

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

but i want to see if its not…
if str[x].chr !=~ /[aeiouy]/

doesnt work though…

You can do like this:
if str[x].chr !~ /[aeiouy]/

Or
unless str[x].chr =~ /[aeiouy]/

Regards,
Park H.

#19

On 2008-05-08, globalrev removed_email_address@domain.invalid wrote:

this way:
if str[x].chr =~ /[aeiouy]/
i can see if a char is any of the chars aeioyu

but i want to see if its not…
if str[x].chr !=~ /[aeiouy]/

how about something like:

if not str[x].chr =~ /[aeiouy]/ then print “YO\n” end

OR

if str[x].chr !~ /[aeiouy]/ then print “YO\n” end

#20

On Wed, May 7, 2008 at 5:19 PM, Todd B. removed_email_address@domain.invalid wrote:

Get a good book on regular expressions. Trust me, it will seriously
help you in the long run for most any type of programming/system
interaction. If you want a quick look at some syntax,
http://www.regular-expressions.info might help. Note that, this is
only one of many ways to do what you are trying to do, but I think it
is the best way for this case.

Hmm…hmm (cough). I was talking to globalrev, not Tim :confused:

Todd