Perl join()函数
Perl中的join()函数将LIST中的元素合并成一个字符串,使用VAR的值来分隔每个元素。它实际上与 split 相反。
注意,VAR只被放在LIST中的成对元素之间;它不会被放在字符串的第一个元素之前或最后一个元素之后。提供一个空字符串,而不是undef,以连接没有分隔符的字符串。
语法: Join(VAR, LIST)
参数
- VAR: 要放在列表元素之间的分隔符。
- LIST:要转换为字符串的LIST。
返回: 返回连接后的字符串。
例子 1 :
#!/usr/bin/perl
# Joining string with a separator
string = join( "-", "Geeks", "for", "Geeks" );
print"Joined String isstring\n";
# Joining string without a separator
string = join( "", "Geeks", "for", "Geeks" );
print"Joined String isstring\n";
输出
Joined String is Geeks-for-Geeks
Joined String is GeeksforGeeks
例2:
#!/usr/bin/perl
# Joining string with '~' separator
string = join( "~", "Geeks", "for", "Geeks" );
print"Joined String isstring\n";
# Joining string with '***' separator
string = join( "***", "Geeks", "for", "Geeks" );
print"Joined String isstring\n";
输出:
Joined String is Geeks~for~Geeks
Joined String is Geeks***for***Geeks