Solved

string manipulation

Posted on 2001-07-27
2
175 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
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 100 total points
ID: 6327959
$str=~s/(.{23})/\1<BR>/g;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

820 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