View Full Version : strings
munna_dude
January 31st, 2007, 12:47 AM
hi all.
i have a string like this
/www.search.yahoo.com/search?/ruby
i would like to print only /search?
how can i do this
please show me the way
thank you
rob
February 1st, 2007, 03:32 AM
To return part of a string that matches another string, use something like this:
"hello world"[/hello/]
Which will return
"hello"
Or, like this:
a = "Hello, World"
a[/hello/i]
(note the "i" means case insensitive, so capitol letters or lower case letters will still be returned)
Hope this helps.
munna_dude
February 2nd, 2007, 12:51 AM
hi
how can i search last 3 chars in the
a ="one.one.one.two.two.two"
can please show me the way
thank you very much
rob
February 2nd, 2007, 04:55 AM
I'm guessing you want to determine the extension of a domain that is in a string?
In that case, something similar to this would work:
a[/\.[a-z]*$/]
This would return whatever is after the last dot in the URL.
munna_dude
February 2nd, 2007, 06:31 AM
thank you
it is great.
working fine.
a="oneoneonetwotwotwothreetwo"
here i hav eto print last three chars.
please show me the way
thank you again
rob
February 4th, 2007, 03:30 AM
That would be:
a[/(...)$/]
You might want to refer to http://www.ruby-doc.org/core/ for documentation on Strings and other ruby objects.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.