📜  Perl字符串

📅  最后修改于: 2021-01-07 08:28:15             🧑  作者: Mango

Perl字串

字符串是Perl语言的重要组成部分。它们是标量变量,因此它们以($)符号开头。可以在单引号(')或双引号(“)中定义字符串。

Perl字符串运算符

运算符可以轻松地以不同方式操作字符串。字符串运算符有两种类型:

  • 串联(。)
  • 重复次数[x]

Perl串联运算符

Perl字符串用(。)符号而不是(+)符号连接。

$firstName = "Christian";
$lastName = "Grey";
$fullName = $firstName . " " . $lastName;
print "$fullName\n";

输出:

Christian Grey

Perl重复运算符

Perl字符串可以使用(x)变量重复多次。

$text = "Thank You ";
$output = $text x 3;
print "$output\n";

输出:

Thank You Thank You Thank You

Perl初始化和声明字符串

在Perl中,要声明一个字符串在变量名之前使用my关键字。

可以使用以下语法初始化和声明字符串:

my $variableName = "";

在此示例中,我们显示了如何初始化和声明字符串。我们通过以下方式一起打印了几个字符串:

  • 使用点(。)运算符连接字符串。
  • 提供字符串作为单独的参数。
  • 将字符串嵌入更大的字符串。

我们已经展示了所有三种print输出的方法。

 use strict;
use warnings;
# Declaring and initializing a string.
my $msg1 = "Welcome at JavaTpoint.";
my $msg2 = "This is our Perl Tutorial.";
#printing using . operator.
print $msg1 . "" . $msg2. "\n";
#print as separate arguments.
print $msg1, "",$msg2, "\n";
#embedd string in a bigger string.
print "$msg1$msg2\n";

输出:

Welcome at JavaTpoint. This is our Perl Tutorial.
Welcome at JavaTpoint. This is our Perl Tutorial.
Welcome at JavaTpoint. This is our Perl Tutorial.

Perl的格式化字符字符串

Character Description
\a Bell
\b Gives a backspace
\cX Control the characters. X is a character.
\e escape next character
\E it ends \u, \l and \q function
\f Gives formfedd to the string
\l Transformation of only next letter into lower case.
\L Transformation of all letters into lower case.
\n Begins next line from a new line
\0nn Octal format numbers are created
\Q Do not match the pattern
\r Gives a carriage return
\t Gives a tab to the string
\u Transformation of only next letter into lower case.
\U Transformation of letters into uppercase.
\xnn Hexadecimal format numbers are created

Perl单引号与双引号字符串

字符串可以放在单引号(')或双引号(“)中,但它们的行为几乎没有什么不同。

my $user = 'Ana';
print 'Hello $user, welcome at our site.\n';
print "\n";
my $user = 'Ana';
my $day  = "today";
print "Hello $user, welcome at our site $day.\n";

输出:

Hello $user, welcome at our site.\n
Hello Ana, welcome at our site today.

单引号中的所有字符均按原样解释。

双引号提供插值。表示字符串存在的其他变量将代表它们的值。转义字符将被其值替换,例如'\ n'将显示换行符。

Perl substr()示例

substr()函数用于截断字符串。我们需要提供一个偏移量字符串。字符串将被截断直到提供的偏移值。

提及偏移量的长度将在偏移值之后print字符串,直到提到的长度为止。

如果提供具有偏移量和长度的新字符串,它将替换偏移量后的字符串,直到长度值为止。

use strict;
use warnings;
# Original string
my $originalstring = "Our site javaTpoint provides all type of tutorials";
print "$originalstring\n";
# Offset of 4
my $offset = substr($originalstring, 4);
print "$offset\n";
# Offset of 4, length 15
my $offsetlength = substr($originalstring, 4, 15);
print "$offsetlength\n";
# Replacing length with the new string
my $replacing = substr($originalstring, 4, 15, "one and only site");
print "$originalstring\n";

输出:

Our site javaTpoint provides all type of tutorials
site "javaTpoint" provides all type of tutorials
site javaTpoint
Our one and only site provides all type of tutorials

Perl字符串比较示例,eq

Perl字符串始终与eq而不是(==)进行比较。它检查两个字符串是否相等。

my $string1 = "Ana";
my $string2 = "Ana";
if($string1 eq $string2) {
    print "Match!\n";
}
my $string3 = "Ana";
my $string4 = "Christian";
if($string3 eq $string4) {
    print "Match\n!";
}else{
    print "Missmatch!\n";
}

输出:

Match!
Missmatch!

Perl确定字符串的长度,length()

Perl字符串的长度可以使用length()函数确定。

my $msg = "Our site javaTpoint provides all type of tutorials";
print "String Length : ", length($msg), "\n";

输出:

String Length : 50

Perl用另一个字符串s /// g替换一个字符串

一个字符串可以用两种方式替换为另一个字符串。

在第一个中,我们用Lions替换了Tigers ,它在字符串用s ///出现。

在第二篇中,我们用s /// g全局替换了花朵的玫瑰。

my $var1 = "Tigers are big and frightening.";
$var1 =~ s/Tigers/Lions/;
print "$var1\n";
my $var2 = "Red roses are very popular. Yellow roses are less seen.";
$var2 =~ s/roses/flowers/g;
print "$var2\n";

输出:

Lions are big and frightening.
Red flowers are very popular. Yellow flowers are less seen.

Perl在字符串找到匹配项,=〜

Perl提供了一个match运算符(=〜)从字符串找到一个子字符串。

my $var = "Tigers are big and frightening.";
if($var =~ /frightening/)
{
    print "Matched\n";
}else{
    print "Match not Found\n";
}
if($var =~ /biggest/)
{
    print "Matched\n";
}else{
    print "Match not Found\n";
}

输出:

Matched
Match not Found

Perl连接两个字符串(。=)

可以使用(。=)运算符将两个字符串连接在一起。

my $str1 = "Where there is a will,";
my $str2 = "there is a way.\n";
my $joining = '';
$joining = $str1 . ' ';
$joining .= $str2;
print $joining;

输出:

Where there is a will, there is a way.