From: Devin Mullins Date: 2005-09-24T10:44:03+09:00 Subject: Re: trimming strings Florian Gro� 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
. I >> imagine I use a loop to count how many characters I get to before it >> finds
? 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("
")] > > You probably do it more efficiently with Regexps, but that involves > 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: ----------------- ==, ===, =~, casefold?, eql?, hash, initialize_copy, inspect, kcode, match, options, source, to_s, ~ C:\Documents and Settings\dlm>ri Regexp.new ------------------------------------------------------------ Regexp::new Regexp.new(string [, options [, lang]]) => regexp Regexp.new(regexp) => regexp Regexp.compile(string [, options [, lang]]) => regexp Regexp.compile(regexp) => 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::POSIXLINE+, _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' = none, `e', `E' = EUC, `s', `S' = SJIS, `u', `U' = UTF-8. r1 = Regexp.new('^a-z+:\s+\w+') #=> /^a-z+:\s+\w+/ r2 = Regexp.new('cat', true) #=> /cat/i r3 = Regexp.new('dog', Regexp::EXTENDED) #=> /dog/x r4 = Regexp.new(r2) #=> /cat/i C:\Documents and Settings\dlm>irb irb(main):001:0> "hello\nfun" =~ /o.*un/ => nil irb(main):002:0> "hello\nfun" =~ /o.*un/m => 4 irb(main):003:0> "This is some text irb(main):004:0" I am typing
Lookee irb(main):005:0" here!
"[0 ... %r{
}] ArgumentError: bad value for range from (irb):5 irb(main):006:0> "This is some text irb(main):007:0" I am typing
Lookee irb(main):008:0" here!
"[0, %r{
}] 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] => 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" a[-3,2] #=> "er" a[-4..-2] #=> "her" a[12..-1] #=> nil a[-2..-4] #=> "" a[/[aeiou](.)\1/] #=> "ell" a[/[aeiou](.)\1/, 0] #=> "ell" a[/[aeiou](.)\1/, 1] #=> "l" a[/[aeiou](.)\1/, 2] #=> nil a["lo"] #=> "lo" a["bye"] #=> 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
Lookee irb(main):003:0" here"[%r{(.*?)
},0] => "I am typing
" irb(main):004:0> "This is some text irb(main):005:0" I am typing
Lookee irb(main):006:0" here"[%r{(.*?)
}m,0] => "This is some text\nI am typing
" irb(main):007:0> "This is some text irb(main):008:0" I am typing
Lookee irb(main):009:0" here"[%r{(.*?)
}m,1] => "This is some text\nI am typing" The End. Devin