Ruby:Find Position

From Progzoo
Jump to: navigation, search

Find first occurance

Use s.index(t) to find where t occurs in s.

s.index("t") gives the character position of the first "t" in s.

It returns nil if not found.

0 1 2 3 4 5 6 7 8 9 10 11 12
O n e t w o t h r e e
f o u r f i v e s i x


[Font] [Default] [Show] [Resize] [History] [Profile]

Find last occurance

Use s.rindex(t) to find the last place where t occurs in s.

s.rindex("t") gives the character position of the last "t".

It returns nil if not found.

0 1 2 3 4 5 6 7 8 9 10 11 12
O n e t w o t h r e e
f o u r f i v e s i x



[Font] [Default] [Show] [Resize] [History] [Profile]

Find occurance after ...

Use s.index(t,n) to find where t occurs in s.

s.index("t",5) gives the character position of the first "t" after position 5.

It returns nil if not found.

0 1 2 3 4 5 6 7 8 9 10 11 12
O n e t w o t h r e e
f o u r f i v e s i x


[Font] [Default] [Show] [Resize] [History] [Profile]