Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding '-' to a string

Status
Not open for further replies.

iamready

Programmer
May 7, 2004
13
US
Hi,

How do i add a '-' (hyphen) at a particular position in a given string for eg

the string is 'perl'
how do i convert it to 'pe-rl'

thanks in advance
harsha
 
Have a look at substr() harsha, that will do it for you.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
You could also do this via regex.

my $string = 'perl';
$string = s/^(.{2})(.)/$1-$2/;

Cheers,

Sloppyhack
 
harsha

As Mike says... I would most certainly suggest you use substr for this. RegEx method is sloppy and just not necessary. You have a [red]SPECIFIC[/red] position so I think it is much more sensible to use substr.


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top