How to concatenate string in Perl

How concatenate string in Perl

Satya Perl Solution , ,


How to concatenate string in Perl: Concatenating a string is very important feature in any computer programming language. Perl provide a very elegant way to join two or more strings into one.

In this post, I will explain how we can join and concatenate two strings into one.

If you want to learn Perl quickly then refer this short guide.

Join

You can use join function to join many strings into one. You can also join elements of an array into one string.

@str_array=("This", " is"," very"," good"," effort");
$joined_string=join('|',@str_array);
print ($joined_string)

Output:

This is very good effort

Concatenate operator(.)

Joining two string using concatenate operator ‘.’, this is also called dot operator.

$str1='Hello';
$str2='world';
$joined_2_str= $str1 . $str2 . "\n";
print ($joined_2_str)

Output:

Hello world

Dot-equal operator

You can also join element of an array by using dot-equal (concatenate assignment ‘.=‘ ) operator to make string.

@array=("This","is","complete","sentence");
foreach $element (@array){
$sentence.= $element ." ";
}
print $sentence . "\n";

Output

This is complete sentence

You May Also Like..

How to remove tags from HTML in Perl

HMost often, in development project, you get requirement to remove tags from HTML page to extract text. Different programming languages […]

Parse Json using Perl module JSON::Parse

Parse Json using Perl module JSON::Parse

Parse Json using Perl module JSON::Parse:  Json is Java script Object Notation. It is a text in Java object notation, […]

How to write excel sheet using Perl module

How to write excel sheet using Perl module : Microsoft excel is an application, which we can use to store […]

Leave a Reply

Your email address will not be published. Required fields are marked *