How to trim a string

D

dany

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.
 
T

Tim Hunter

Dany said:
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"
 
7

7stud --

Dany said:
...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 <----
 
D

Dany Wu

... said:
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top