珀尔 |加入()函数
Perl 中的 join()函数将 LIST 的元素组合成一个字符串,使用 VAR 的值来分隔每个元素。它实际上与拆分相反。
请注意,VAR 仅放置在 LIST 中的元素对之间;它不会放在字符串的第一个元素之前或最后一个元素之后。提供一个空字符串而不是 undef,以便在没有分隔符的情况下将字符串连接在一起。
Syntax: join(VAR, LIST)
Parameters:
- VAR : Separator to be placed between list elements.
- LIST : LIST to be converted into String.
Return: Returns the joined string
示例 1:
#!/usr/bin/perl
# Joining string with a separator
$string = join( "-", "Geeks", "for", "Geeks" );
print"Joined String is $string\n";
# Joining string without a separator
$string = join( "", "Geeks", "for", "Geeks" );
print"Joined String is $string\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 is $string\n";
# Joining string with '***' separator
$string = join( "***", "Geeks", "for", "Geeks" );
print"Joined String is $string\n";
输出 :
Joined String is Geeks~for~Geeks
Joined String is Geeks***for***Geeks