?
Solved

string manipulation

Posted on 2001-07-27
2
Medium Priority
?
192 Views
Last Modified: 2010-03-05
hello experts!
how do i insert a '<br>' every 24th character in a string?
0
Comment
Question by:nikitin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Expert Comment

by:cidaris
ID: 6327958
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";


0
 
LVL 8

Accepted Solution

by:
bebonham earned 400 total points
ID: 6327959
$str=~s/(.{23})/\1<BR>/g;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question