Florian said:
jotto said:
I want to trim the string for the text that comes with each entry in
the database. Ideally I want to trim it where it first finds <br />. I
imagine I use a loop to count how many characters I get to before it
finds <br />? Then I use some function (a function I can't find) that
trims the string to the desired amount of characters.
Try this: str[0 ... str.index("<br />")]
You probably do it more efficiently with Regexps, but that involves=20
getting the multi-line flag right.
Devin's experiments with figuring this stuff out:
C:\Documents and Settings\dlm>ri Regexp
---------------------------------------------------------- Class: Regexp
Document-class: Regexp
A +Regexp+ holds a regular expression, used to match a pattern
against strings. Regexps are created using the +/.../+ and
+%r{...}+ literals, and by the +Regexp::new+ constructor.
------------------------------------------------------------------------
Class methods:
--------------
compile, escape, last_match, new, quote, union
Instance methods:
-----------------
=3D=3D, =3D=3D=3D, =3D~, casefold?, eql?, hash, initialize_copy, ins=
pect,
kcode, match, options, source, to_s, ~
C:\Documents and Settings\dlm>ri Regexp.new
------------------------------------------------------------ Regexp::new
Regexp.new(string [, options [, lang]]) =3D> regexp
Regexp.new(regexp) =3D> regexp
Regexp.compile(string [, options [, lang]]) =3D> regexp
Regexp.compile(regexp) =3D> regexp
------------------------------------------------------------------------
Constructs a new regular expression from _pattern_, which can be
either a +String+ or a +Regexp+ (in which case that regexp's
options are propagated, and new options may not be specified (a
change as of Ruby 1.8). If _options_ is a +Fixnum+, it should be
one or more of the constants +Regexp::EXTENDED+,
+Regexp::IGNORECASE+, and +Regexp:
OSIXLINE+, _or_-ed together.
Otherwise, if _options_ is not +nil+, the regexp will be case
insensitive. The _lang_ parameter enables multibyte support for the
regexp: `n', `N' =3D none, `e', `E' =3D EUC, `s', `S' =3D SJIS, `u',=
`U'
=3D UTF-8.
r1 =3D Regexp.new('^a-z+:\s+\w+') #=3D> /^a-z+:\s+\w+/
r2 =3D Regexp.new('cat', true) #=3D> /cat/i
r3 =3D Regexp.new('dog', Regexp::EXTENDED) #=3D> /dog/x
r4 =3D Regexp.new(r2) #=3D> /cat/i
C:\Documents and Settings\dlm>irb
irb(main):001:0> "hello\nfun" =3D~ /o.*un/
=3D> nil
irb(main):002:0> "hello\nfun" =3D~ /o.*un/m
=3D> 4
irb(main):003:0> "This is some text
irb(main):004:0" I am typing<br />Lookee
irb(main):005:0" here!<br />"[0 ... %r{<br />}]
ArgumentError: bad value for range
from (irb):5
irb(main):006:0> "This is some text
irb(main):007:0" I am typing<br />Lookee
irb(main):008:0" here!<br />"[0, %r{<br />}]
TypeError: cannot convert Regexp into Integer
from (irb):8:in `[]'
from (irb):8
irb(main):009:0>
C:\Documents and Settings\dlm>ri "String#[]"
-------------------------------------------------------------- String#[]
str[fixnum] =3D> fixnum or nil
str[fixnum, fixnum] =3D> new_str or nil
str[range] =3D> new_str or nil
str[regexp] =3D> new_str or nil
str[regexp, fixnum] =3D> new_str or nil
str[other_str] =3D> new_str or nil
str.slice(fixnum) =3D> fixnum or nil
str.slice(fixnum, fixnum) =3D> new_str or nil
str.slice(range) =3D> new_str or nil
str.slice(regexp) =3D> new_str or nil
str.slice(regexp, fixnum) =3D> new_str or nil
str.slice(other_str) =3D> 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 =3D "hello there"
a[1] #=3D> 101
a[1,3] #=3D> "ell"
a[1..3] #=3D> "ell"
a[-3,2] #=3D> "er"
a[-4..-2] #=3D> "her"
a[12..-1] #=3D> nil
a[-2..-4] #=3D> ""
a[/[aeiou](.)\1/] #=3D> "ell"
a[/[aeiou](.)\1/, 0] #=3D> "ell"
a[/[aeiou](.)\1/, 1] #=3D> "l"
a[/[aeiou](.)\1/, 2] #=3D> nil
a["lo"] #=3D> "lo"
a["bye"] #=3D> nil
C:\Documents and Settings\dlm>\irb
'\irb' is not recognized as an internal or external command,
operable program or batch file.
C:\Documents and Settings\dlm>irb
irb(main):001:0> "This is some text
irb(main):002:0" I am typing<br />Lookee
irb(main):003:0" here"[%r{(.*?)<br />},0]
=3D> "I am typing<br />"
irb(main):004:0> "This is some text
irb(main):005:0" I am typing<br />Lookee
irb(main):006:0" here"[%r{(.*?)<br />}m,0]
=3D> "This is some text\nI am typing<br />"
irb(main):007:0> "This is some text
irb(main):008:0" I am typing<br />Lookee
irb(main):009:0" here"[%r{(.*?)<br />}m,1]
=3D> "This is some text\nI am typing"
The End.
Devin