珀尔 |有用的字符串运算符
Perl 中的字符串是一个标量变量,以 ($) 符号开头,它可以包含字母、数字、特殊字符。字符串可以由单个单词、一组单词或多行段落组成。字符串由用户在单引号 (') 或双引号 (") 内定义。
运算符是任何编程语言的基础,Perl 也是如此。用户可以将 String 中的运算符定义为有助于对操作数执行特定数学和逻辑计算的符号。这些操作类似于连接、比较、替换等。
例子:
Perl
# Perl program to demonstrate the
# Concatenation Operator(.) in String
#!/usr/bin/perl
# Input first string
$first_string = "Geeks";
# Input second string
$second_string = "forGeeks";
# Implement Concatenation operator(.)
$concat_string = $first_string.$second_string;
# displaying concatenation string result
print "String After Concatenation = $concat_string\n";
String After Concatenation = GeeksforGeeks
下面列出了 Perl 中一些对字符串操作有用的运算符:
Operator Description qw ‘quote word’ operator is used to extract each element of the given string as it is in an array of elements in single-quote ( ‘ ‘ ) q Used in place of single quotes. It uses a set of parentheses to surround the string qq Used in place of double quotes. It uses a set of parentheses to surround the string y Translates all characters of SearchList into the corresponding characters of ReplacementList tr Similar to ‘y’ operator it translates all characters of SearchList into the corresponding characters of ReplacementList eq Used to check if the string to its left is stringwise equal to the string to its right ne Used to check if the string to its left is stringwise not equal to the string to its right le Used to check if the string to its left is stringwise less than or equal to the string to its right ge Used to check if the string to its left is stringwise greater than or equal to the string to its right lt Used to check if the string to its left is stringwise less than the string to its right gt Used to check if the string to its left is stringwise greater than the string to its right cmp Used to compare if the two strings placed left and right to this operator are equal or less than the other substitution operator(s) Used to substitute a text of the string with some pattern specified by the user matching operator(m) Used to match a pattern within the given text