Forum: Ruby Ask for some experience

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E13ba8e702211be152c640df2d8e141f?d=identicon&s=25 Joe Black (joe-black)
on 2005-11-15 14:57
hello everyone.
any one like to share some experience about ruby string
just like java: trim(), split(), or indexOf()

all regards
159cd33667611645164e8623de42f67e?d=identicon&s=25 Toby Dipasquale (toby)
on 2005-11-15 15:03
joe-black wrote:
> hello everyone.
> any one like to share some experience about ruby string
> just like java: trim(), split(), or indexOf()

Java split() => Ruby String.split:

"hello world".split( " ")

returns [ "hello", "world" ].

Java trim() => Ruby String.strip:

"    hello world          ".strip

returns "hello world".

Java indexOf() => Ruby String.index

"hello world".index( "w")

returns 6.
E13ba8e702211be152c640df2d8e141f?d=identicon&s=25 Joe Black (joe-black)
on 2005-11-15 17:09
that's cool.
any more? the frequently used.
04bd97396a23bebf19887fd041e15123?d=identicon&s=25 zoso (Guest)
on 2005-11-15 17:48
(Received via mailing list)
On Wed, Nov 16, 2005 at 01:09:17AM +0900, Joe Black wrote:
> that's cool.
> any more? the frequently used.

irb(main):001:0> "foo".upcase
=> "FOO"
irb(main):002:0> "foo".sub(/[aeiou]/, 'a')
=> "fao"
irb(main):003:0> "foo".gsub(/[aeiou]/, 'a')
=> "faa"
irb(main):004:0> "FOO".downcase
=> "foo"
irb(main):005:0> "foo".capitalize
=> "Foo"
irb(main):006:0> "foo".tr("aeiou", "QWRTY")
=> "fTT"
irb(main):007:0> "foo".rjust(10)
=> "       foo"
irb(main):008:0> String.help

The last one only works if you have ihelp installed. If not, install it
or do

"foo".methods.sort
6b4566518f6675477dab9b8ba813cf3c?d=identicon&s=25 ruby.brian (Guest)
on 2005-11-16 10:08
(Received via mailing list)
On 15/11/05, Joe Black <allenbobo@gmail.com> wrote:
> hello everyone.
> any one like to share some experience about ruby string
> just like java: trim(), split(), or indexOf()
>
> all regards
>
>
>

Reading the pickaxe and
$ ri String
would be a good idea.

One neat thing I onlz found a few month ago is this
irb(main):001:0> "my string with 28 characters"[/\d+/]
=> "28"
irb(main):002:0> s = "my string with 28 characters"
=> "my string with 28 characters"
irb(main):003:0> bschroed@black:~/svn$ irb1.8
irb(main):001:0> s = "my string with 28 characters"
=> "my string with 28 characters"
irb(main):002:0> s[/\d+/]
=> "28"
irb(main):003:0> s[/\d+/] = "twentyeight"
=> "twentyeight"
irb(main):004:0> s
=> "my string with twentyeight characters"
irb(main):005:0> s[/string (\w+) (\w+)/]
=> "string with twentyeight"
irb(main):006:0> s[/string (\w+) (\w+)/, 1]
=> "with"
irb(main):007:0> s[/string (\w+) (\w+)/, 2]
=> "twentyeight"

Especially line three is really cool.

cheers,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
This topic is locked and can not be replied to.