企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Perl - 字符串 > 原文: [https://beginnersbook.com/2018/03/perl-strings/](https://beginnersbook.com/2018/03/perl-strings/) 字符串是一系列字符。正如我们在标量指南中所讨论的那样,字符串被认为是 Perl 编程语言中的标量变量。有两种方法可以使用`'`(单引号)和`"`(双引号)在 Perl 中定义字符串。 ## 1. Perl 中的字符串声明和初始化 由于字符串是标量,它们的名称以`$`开头。**例如:** ```perl my $website = "BeginnersBook"; my $editor = "Chaitanya"; print "$editor works for $website"; ``` 输出: ```perl Chaitanya works for BeginnersBook ``` ## 2 字符串插值 - 单引号和双引号 我已经在[ Perl 标量的文章](https://beginnersbook.com/2017/05/scalars-in-perl/)中介绍了这个主题。 引号不是字符串的一部分,它们只是指定字符串的开头和结尾。您可能认为**单引号和双引号**都有相同的用途,但事实并非如此。它们用于不同的 2 例。要理解这两者之间的区别,让我们看一下下面的例子。 ```perl #!/usr/bin/perl print "Welcome to\t Beginnersbook.com\n"; print 'Welcome to\t Beginnersbook.com\n'; This would produce the following 输出: Welcome to Beginnersbook.com Welcome to\t Beginnersbook.com\n ``` 您可以清楚地看到双引号内插转义序列`\t`和`\n`的区别,但是单引号没有。 **注意:**我们将在下一个教程中详细讨论转义序列。 **例 2:单引号和双引号:** ```perl #!/usr/bin/perl $website = "Beginnersbook"; print "Website Name: $website\n"; print 'Website Name: $website\n'; ``` **输出:** ```perl Website Name: Beginnersbook Website Name: $website\n ``` 这是双引号的另一个优点,因为它们是**“可变插值”**。这意味着双引号内的变量名称将替换为其值。单引号字符串中不会发生这种情况。 ### 2.1 单引号的使用 您可能正在考虑避免在 perl 程序中使用单引号,但在某些情况下您可能希望使用单引号。 例如如果你想在变量中存储**电子邮件地址**,那么双引号会引发错误,在这种情况下你需要使用单引号。对于例如 ```perl $email = "[email protected]"; # would throw an error $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"`然后我需要使用以下逻辑: ```perl #!/usr/bin/perl $msg = "Hello This is \"My blog\""; print "$msg\n"; ``` **输出:** ```perl Hello This is "My blog" ``` ## 3.字符串操作 让我们看看我们可以对字符串执行的操作。 ### 3.1 连接 要连接字符串,请使用点(`.`)运算符。在下面的例子中,我们连接三个字符串,`$str1`,空格和`$str2`。 ```perl $str1 = "Cherry"; $str2 = "Coco"; $mystr = $str1 . " " . $str2; print "$mystr\n"; ``` 输出: ```perl Cherry Coco ``` ### 3.2 重复 要重复具有指定次数的字符串,请使用字符`x`后跟数字。在下面的示例中,我们使用了字符`x`之后的数字 4,这就是字符串在输出中重复四次的原因。 ```perl $str = "Cherry"; $mystr = $str x 4; print "$mystr\n"; ``` 输出: ```perl CherryCherryCherryCherry ``` ### 3.3 获取子串 - `substr()`函数 `substr()`函数用于从给定字符串中获取子字符串。 ```perl use strict; use warnings; # This is our original string my $str = "I know who you are, I will find you"; print "My original String is: $str\n"; # substring - starting from 8th char till the end of string my $substr1 = substr($str, 7); print "My First substring is: $substr1\n"; # substring - starting from 8th char and length of 11 my $substr2 = substr($str, 7, 11); print "My Second substring is: $substr2\n"; ``` 输出: ```perl My original String is: I know who you are, I will find you My First substring is: who you are, I will find you My Second substring is: who you are ``` 该相同功能可用于用新内容替换字符串的一部分。让我们举个例子来理解这个: ```perl use strict; use warnings; # This is our original string my $str = "I know who you are, I will find you"; print "My original String is: $str\n"; my $newstr = substr($str, 7, 11, "you are the killer"); print "My updated String is: $str\n" ``` 输出: ```perl My original String is: I know who you are, I will find you My updated String is: I know you are the killer, I will find you ``` ### 3.4 在 Perl - `length()`函数中查找字符串的长度 要在 Perl 中查找字符串的长度,请使用`length()`函数。此函数计算字符串中的字符数(包括空格)并返回它。 ```perl use strict; use warnings; my $str = "I know who you are, I will find you"; print "length of my string is:", length($str); ``` 输出: ```perl length of my string is:35 ``` ### 3.5 Perl 中的字符串比较 为了比较两个字符串,我们使用 Perl 中的[`eq`运算符](https://beginnersbook.com/2017/02/perl-operators-complete-guide/)。 ```perl use strict; use warnings; my $str = "hello"; my $str2 = "hello"; if ($str eq $str2){ print "str and str2 are same\n"; } ``` 输出: ```perl str and str2 are same ```