Check if char in string?

G

globalrev

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.
 
M

Marc Heiler

globalrev said:
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'
 
G

globalrev

str = "you muppet"
puts "its in there" if str.include? 'y'


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

Joel VanderWerf

globalrev said:
On 7 Maj, 19:39, (e-mail address removed) (matt neuburg) 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.

Try the suggestion, you may be surprised :)
 
D

Daniel Finnie

[Note: parts of this message were removed to make it a legal post.]

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
 
T

Todd Benson

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
 
G

globalrev

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?
 
J

Joel VanderWerf

globalrev said:
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?

str =~ /[aeiouy]/

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

Tim Hunter

globalrev said:
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?

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.
 
T

Todd Benson

globalrev said:
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?

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
 
T

Todd Benson

globalrev wrote:

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.

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

Todd
 
G

globalrev

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?
 
H

Heesob Park

Hi,
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 Heesob
 
J

Jim Cochrane

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


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top