Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: Search a word in a string
Vaggelis Typaldos
11/5/2008 9:47:00 PM
Ganesh Ganesh wrote:
> Is there any ruby supported methods is there for searching a word in a
> particular string
>
> For Example "Hi this is for test"
> i need to search "Hi"
Scan is the more appropriate method:
irb(main):001:0> s = "Hi this is for test"
=> "Hi this is for test"
irb(main):002:0> s.scan("Hi")
=> ["Hi"] # creates an array if the required word matches
Otherwise you can use regexp with a conditional:
irb(main):003:0> if s=~ /Hi/
irb(main):004:1> puts "found"
irb(main):005:1> end
found
include? is another solution:
irb(main):007:0> s.include?("Hi")
=> true
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Search a word in a string
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password