in reply to How to split a string into two lines, intelligently
The regular expression will break the line on any whitespace (space, tab, etc.) but rindex will break only on a space character.#!/usr/bin/perl -w # use strict; my $string = "the quick brown fox jumped over the lazy dog"; # Using a regular expression $string =~ m/(.{0,50}\s)(.*)/; print "$1\n$2\n"; # Using rindex() and substr() my $pos = rindex($string, ' ', 50); print substr($string,0,$pos) . "\n" . substr($string,$pos+1) . "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to split a string into two lines, intelligently
by kweise (Novice) on Oct 10, 2008 at 15:54 UTC |