Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > trimming strings

Reply
Thread Tools

trimming strings

 
 
jotto
Guest
Posts: n/a
 
      09-24-2005
folks->

i am using RoR's scaffolding for a very basic blog setup.

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.

Any input would be greatly appreciated.

 
Reply With Quote
 
 
 
 
Florian Groß
Guest
Posts: n/a
 
      09-24-2005
jotto wrote:

> 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
getting the multi-line flag right.

Oh, or this: str.split("<br />").first



 
Reply With Quote
 
 
 
 
Joe Van Dyk
Guest
Posts: n/a
 
      09-24-2005
On 9/23/05, jotto <> wrote:
> folks->
>
> i am using RoR's scaffolding for a very basic blog setup.
>
> 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.


Your question is sort of unclear. So I'm not really sure what you're
asking for. But perhaps the following will help you:

str =3D "some very long string that has lots of text in it. <br />"
str << "more long boring text that has lots of text in it. <br />"

puts "The string contains: '#{ str }'"
puts
puts "Number of characters before first <br /> tag: "
puts str.split("<br />").first.length
puts
puts "Trimming to 30 characters: "
puts str[0..30]


OUTPUTS
The string contains: 'some very long string that has lots of text in it. <b=
r />m
ore long boring text that has lots of text in it. <br />'

Number of characters before first <br /> tag:
51

Trimming to 30 characters:
some very long string that has


 
Reply With Quote
 
Devin Mullins
Guest
Posts: n/a
 
      09-24-2005
Florian Gro=DF wrote:

> jotto wrote:
>
>> 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.. 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Help trimming URL path =?Utf-8?B?Q2hyaXM=?= ASP .Net 4 12-14-2004 06:56 PM
trimming text in a datagrid to match column length Brian Henry ASP .Net 5 10-21-2004 07:16 PM
Trimming data in dropdownlist from sqldatareader William Mild ASP .Net 2 10-23-2003 12:51 PM
Re: Trimming Blank Spaces in String Steve C. Orr, MCSD ASP .Net 1 08-09-2003 12:46 AM



Advertisments