珀尔 |有用的字符串函数
Perl 中的字符串是一个标量变量,以 ($) 符号开头,它可以包含字母、数字、特殊字符。字符串可以由单个单词、一组单词或多行段落组成。字符串由用户在单引号 (') 或双引号 (") 内定义。
Perl 提供了各种函数来像任何其他编程语言一样操作字符串。
例子:
# Perl program to demonstrate
# string length function
# string
my $s = "geeksforgeeks";
# using length function &
# displaying length
print("Length: ", length($s));
# Converting to Uppercase
print("\nTo Upper Case: ");
print(uc($s), "\n");
输出:
Length: 13
To Upper Case: GEEKSFORGEEKS
Perl 的一些字符串函数如下:
Function | Description |
---|---|
chomp() | Used to remove the last trailing newline from the input string |
length() | Finds length (number of characters) of a given string, or $_ if not specified |
substr() | Returns a substring out of the string passed to the function starting from a given index up to the length specified |
uc() | Returns the string passed to it after converting it into uppercase |
ucfirst() | Returns the string VAR or $_ after converting the First character to uppercase |
lc() | Returns a lowercased version of VAR, or $_ if VAR is omitted |
lcfirst() | Returns the string VAR or $_ after converting the First character to lowercase |
chr() | Returns a string representing a character whose Unicode code point is an integer |
chop() | Returns a string representing a character whose Unicode code point is an integer |
index() | Returns the position of the first occurrence of given substring (or pattern) in a string (or text) |
rindex() | Returns the position of the last occurrence of the substring (or pattern) in the string (or text) |
sprintf() | Uses Format provided by the user to return the formatted string with the use of the values in the list |
ord() | Returns the ASCII value of the first character of a string |
quotemeta() | It escapes all meta-characters in the value passed to it as parameter |
split() | Used to split or cut a string into smaller sections or pieces |