原文: https://beginnersbook.com/2018/03/perl-strings/

字符串是一系列字符。正如我们在标量指南中所讨论的那样,字符串被认为是 Perl 编程语言中的标量变量。有两种方法可以使用'(单引号)和"(双引号)在 Perl 中定义字符串。

1. Perl 中的字符串声明和初始化

由于字符串是标量,它们的名称以$开头。例如:

  1. my $website = "BeginnersBook";
  2. my $editor = "Chaitanya";
  3. print "$editor works for $website";

输出:

  1. Chaitanya works for BeginnersBook

2 字符串插值 - 单引号和双引号

我已经在 Perl 标量的文章中介绍了这个主题。

引号不是字符串的一部分,它们只是指定字符串的开头和结尾。您可能认为单引号和双引号都有相同的用途,但事实并非如此。它们用于不同的 2 例。要理解这两者之间的区别,让我们看一下下面的例子。

  1. #!/usr/bin/perl
  2. print "Welcome to\t Beginnersbook.com\n";
  3. print 'Welcome to\t Beginnersbook.com\n';
  4. This would produce the following 输出:
  5. Welcome to Beginnersbook.com
  6. Welcome to\t Beginnersbook.com\n

您可以清楚地看到双引号内插转义序列\t\n的区别,但是单引号没有。

注意:我们将在下一个教程中详细讨论转义序列。

例 2:单引号和双引号:

  1. #!/usr/bin/perl
  2. $website = "Beginnersbook";
  3. print "Website Name: $website\n";
  4. print 'Website Name: $website\n';

输出:

  1. Website Name: Beginnersbook
  2. Website Name: $website\n

这是双引号的另一个优点,因为它们是“可变插值”。这意味着双引号内的变量名称将替换为其值。单引号字符串中不会发生这种情况。

2.1 单引号的使用

您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。
例如如果你想在变量中存储电子邮件地址,那么双引号会引发错误,在这种情况下你需要使用单引号。对于例如

  1. $email = "[email protected]"; # would throw an error
  2. $email = '[email protected]'; # would work fine.

这里的问题是@gmail内插为数组。以@符号开头的变量被内插为数组。我们还可以通过使用\转义序列避免双引号错误,我们已在下一节中讨论过。

2.2 Perl 中的反斜杠

反斜杠可以执行以下两个任务之一:

1)它消除了跟随它的字符的特殊含义。对于例如打印\$myvar会产生$myvar输出而不是变量myvar值,因为$前面的反斜杠(\)消除了$的特殊含义

2)它是反斜杠或转义序列的开始。对于例如\n是用于换行的转义序列

2.2.1 有什么用?

相信我,你会在 perl 中开发应用时经常使用它。假设您要打印包含双引号的字符串。对于例如如果我想打印:Hello This is "My blog"然后我需要使用以下逻辑:

  1. #!/usr/bin/perl
  2. $msg = "Hello This is \"My blog\"";
  3. print "$msg\n";

输出:

  1. Hello This is "My blog"

3.字符串操作

让我们看看我们可以对字符串执行的操作。

3.1 连接

要连接字符串,请使用点(.)运算符。在下面的例子中,我们连接三个字符串,$str1,空格和$str2

  1. $str1 = "Cherry";
  2. $str2 = "Coco";
  3. $mystr = $str1 . " " . $str2;
  4. print "$mystr\n";

输出:

  1. Cherry Coco

3.2 重复

要重复具有指定次数的字符串,请使用字符x后跟数字。在下面的示例中,我们使用了字符x之后的数字 4,这就是字符串在输出中重复四次的原因。

  1. $str = "Cherry";
  2. $mystr = $str x 4;
  3. print "$mystr\n";

输出:

  1. CherryCherryCherryCherry

3.3 获取子串 - substr()函数

substr()函数用于从给定字符串中获取子字符串。

  1. use strict;
  2. use warnings;
  3. # This is our original string
  4. my $str = "I know who you are, I will find you";
  5. print "My original String is: $str\n";
  6. # substring - starting from 8th char till the end of string
  7. my $substr1 = substr($str, 7);
  8. print "My First substring is: $substr1\n";
  9. # substring - starting from 8th char and length of 11
  10. my $substr2 = substr($str, 7, 11);
  11. print "My Second substring is: $substr2\n";

输出:

  1. My original String is: I know who you are, I will find you
  2. My First substring is: who you are, I will find you
  3. My Second substring is: who you are

该相同功能可用于用新内容替换字符串的一部分。让我们举个例子来理解这个:

  1. use strict;
  2. use warnings;
  3. # This is our original string
  4. my $str = "I know who you are, I will find you";
  5. print "My original String is: $str\n";
  6. my $newstr = substr($str, 7, 11, "you are the killer");
  7. print "My updated String is: $str\n"

输出:

  1. My original String is: I know who you are, I will find you
  2. My updated String is: I know you are the killer, I will find you

3.4 在 Perl - length()函数中查找字符串的长度

要在 Perl 中查找字符串的长度,请使用length()函数。此函数计算字符串中的字符数(包括空格)并返回它。

  1. use strict;
  2. use warnings;
  3. my $str = "I know who you are, I will find you";
  4. print "length of my string is:", length($str);

输出:

  1. length of my string is:35

3.5 Perl 中的字符串比较

为了比较两个字符串,我们使用 Perl 中的eq运算符

  1. use strict;
  2. use warnings;
  3. my $str = "hello";
  4. my $str2 = "hello";
  5. if ($str eq $str2){
  6. print "str and str2 are same\n";
  7. }

输出:

  1. str and str2 are same