Link to home
Start Free TrialLog in
Avatar of nikitin
nikitin

asked on

string manipulation

hello experts!
how do i insert a '<br>' every 24th character in a string?
Avatar of cidaris
cidaris

Hopefully, this does what you're looking for.  Obviously, just replace the example string.  Also, I don't know if you want to insert before/after/replace the 24th character, but only slight modifications would be needed.

Thanks
cidaris

$oldstring = "1234567890123456789012345678901234567890123456789012345678901234567890";

my @newstring = split(//,$oldstring,-1);
print "@newstring\n";
print "$newstring[5]\n";
print "$newstring[24]\n";

for (my $intX = 0; $newstring[$intX] ne ""; $intX++){
   if (($intX != 0) && ($intX % 24 == 0)){
      # character is not 0 and is a multiple of 24
      $char = "\<br\>" . "$newstring[$intX]";
   }
   else {
      $char = "$newstring[$intX]";
   }
$out_string = $out_string . $char;
}

print "$out_string\n";


ASKER CERTIFIED SOLUTION
Avatar of bebonham
bebonham

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial