I try to split a line that is separated by 'tab' and expect return an
array with all the elements containing empty space. But my code doesn't
work. Any idea?
I try to split a line that is separated by 'tab' and expect return an
array with all the elements containing empty space. But my code doesn't
work. Any idea?
Thanks,
Li
C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s='\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"
These aren't tabs. They're "\" followed by "t", you can tell because the "\" is backslashed, meaning it is a literal backslash.
I try to split a line that is separated by 'tab' and expect return an
array with all the elements containing empty space. But my code doesn't
work. Any idea?
Thanks,
Li
C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s='\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"
irb(main):002:0> s.split(/\t/)
=> ["\\t\\t\\t\\t\\t\\t\\t\\t"]
irb(main):003:0> s.split(/\\t/)
=>
irb(main):004:0>
--
Posted via http://www.ruby-forum.com/\.
Single quotes treat things literally, like you can't interpolate, and \t is
treated as two separate characters.
$irb
RUBY_VERSION
=> "1.8.6"
s = '\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"
s = "\t\t\t\t\t\t\t\t"
=> "\t\t\t\t\t\t\t\t"
s.split /\t/
=>
'abc#{2}def'
=> "abc\#{2}def"
"abc#{2}def"
=> "abc2def"
···
On Wed, Oct 7, 2009 at 4:29 PM, Li Chen <chen_li3@yahoo.com> wrote:
str.split takes two parameters: the pattern and optionally the "limit"
From the docs:
If the limit parameter is omitted, trailing null fields are suppressed.
If limit is a positive number, at most that number of fields will be
returned (if limit is 1, the entire string is returned as the only entry
in an array). If negative, there is no limit to the number of fields
returned, and trailing null fields are not suppressed.
str.split takes two parameters: the pattern and optionally the "limit"
From the docs:
If the limit parameter is omitted, trailing null fields are suppressed.
If limit is a positive number, at most that number of fields will be
returned (if limit is 1, the entire string is returned as the only entry
in an array). If negative, there is no limit to the number of fields
returned, and trailing null fields are not suppressed.