in reply to Substring consisting of all the characters until "character X"?

Something like:
my $i = 2; substr($fullstring, $i, index($fullstring, ' ', $i) - $i);
should do.

Or use a regexp.

  • Comment on Re: Substring consisting of all the characters until "character X"?
  • Download Code

Replies are listed 'Best First'.
Re^2: Substring consisting of all the characters until "character X"?
by jwkrahn (Abbot) on Mar 07, 2011 at 13:01 UTC

    You should first test that the return value from index is greater than $i or you may get unexpected results.

Re^2: Substring consisting of all the characters until "character X"?
by TheMartianGeek (Acolyte) on Mar 07, 2011 at 14:50 UTC

    substr($fullstring, $i, index($fullstring, ' ', $i) - $i);

    I'll go with that (with the necessary check to make sure that the return value of index() is greater than $i). If I've learned one thing about regular expressions, it's that they start out easy enough to understand but get complicated and confusing very quickly.