[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to trim a string

dany

10/30/2007 12:54:00 AM

Hi everyone,

I'm a newbie who's just started experimenting with Ruby and Rails. At
the moment I'm looking for a way to return an x number of characters
from a string, eg. 5 characters starting from position 10 in the string,
the last 10 chars, the first 15 chars, etc.

Is there a String method that does this? I can't seem to find any but I
may have missed something glaringly obvious. Any help would be greatly
appreciated.

Cheers,
Dany.
--
Posted via http://www.ruby-....

3 Answers

Tim Hunter

10/30/2007 1:09:00 AM

0

Dany Wu wrote:
> Hi everyone,
>
> I'm a newbie who's just started experimenting with Ruby and Rails. At
> the moment I'm looking for a way to return an x number of characters
> from a string, eg. 5 characters starting from position 10 in the string,
> the last 10 chars, the first 15 chars, etc.
>
> Is there a String method that does this? I can't seem to find any but I
> may have missed something glaringly obvious. Any help would be greatly
> appreciated.
>
> Cheers,
> Dany.

String#[] takes a variety of arguments that support the activities you
describe.

-------------------------------------------------------------- String#[]
str[fixnum] => fixnum or nil
str[fixnum, fixnum] => new_str or nil
str[range] => new_str or nil
str[regexp] => new_str or nil
str[regexp, fixnum] => new_str or nil
str[other_str] => new_str or nil
str.slice(fixnum) => fixnum or nil
str.slice(fixnum, fixnum) => new_str or nil
str.slice(range) => new_str or nil
str.slice(regexp) => new_str or nil
str.slice(regexp, fixnum) => new_str or nil
str.slice(other_str) => new_str or nil
------------------------------------------------------------------------
Element Reference---If passed a single Fixnum, returns the code of
the character at that position. If passed two Fixnum objects,
returns a substring starting at the offset given by the first, and
a length given by the second. If given a range, a substring
containing characters at offsets given by the range is returned.
In all three cases, if an offset is negative, it is counted from
the end of str. Returns nil if the initial offset falls outside
the string, the length is negative, or the beginning of the range
is greater than the end.

If a Regexp is supplied, the matching portion of str is returned.
If a numeric parameter follows the regular expression, that
component of the MatchData is returned instead. If a String is
given, that string is returned if it occurs in str. In both cases,
nil is returned if there is no match.

a = "hello there"
a[1] #=> 101
a[1,3] #=> "ell"
a[1..3] #=> "ell"

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

7stud --

10/30/2007 3:03:00 AM

0

Dany Wu wrote:
> ...I'm looking for a way to return an x number of characters
> from a string, eg. 5 characters starting from position 10 in the string,
> the last 10 chars, the first 15 chars, etc.
>
> Is there a String method that does this? I can't seem to find any but I
> may have missed something glaringly obvious. Any help would be greatly
> appreciated.
>

str = "In the absence of justice, what is sovereignty but organized
robbery?"

#5 characters starting from position 10 in the string:

sub_str = str[10, 5] #first char is pos 0, so pos 10 is 11th char
puts sub_str + "<----"

--output:--
ence <----



#the last 10 chars:

sub_str = str[-10..-1]
puts sub_str + "<----"

--output:--
d robbery?



#the first 15 chars:

sub_str = str[0...15] #notice the 3 dots v. 2 dots
puts sub_str + "<----"

--output:--
In the absence <----
--
Posted via http://www.ruby-....

dany

10/30/2007 3:22:00 AM

0

7stud -- wrote:
> Dany Wu wrote:
>> ...I'm looking for a way to return an x number of characters
>> from a string, eg. 5 characters starting from position 10 in the string,
>> the last 10 chars, the first 15 chars, etc.
>>
>> Is there a String method that does this? I can't seem to find any but I
>> may have missed something glaringly obvious. Any help would be greatly
>> appreciated.
>>
>
...<snipped> Some excellent examples </snipped>
>
> #the first 15 chars:
>
> sub_str = str[0...15] #notice the 3 dots v. 2 dots
> puts sub_str + "<----"
>
> --output:--
> In the absence <----

Thank you very much for the detailed explanation. Much appreciated.

Cheers,
Dany.
--
Posted via http://www.ruby-....