This article focuses on the usage of Perl string processing functions. Each function has its own characteristics and functions. Friends in need can refer to it
Please read the detailed introduction of this article below.
Perl string processing function
1. index
Syntax:
position=index(string,substring,position);
Semantics:
Returns the position of substring in string, or -1 if it does not exist.
The parameter position is optional, indicating the number of characters skipped before matching, or starting from this position.
2. rindex
Syntax:
position=rindex(string,substring,position);
Semantics:
Similar to index, the difference is that from the right end match.
3. length
Syntax:
num=length(string);
Semantics:
Returns the length of the string, or the number of characters it contains.
4. pos
Syntax:
offset=pos(string);
Semantics:
Returns the position of the last pattern match.
5.substr
Syntax:
substr(expr,skipchars,length)
Semantics:
Extract string (or string generated by expression ) substring in expr,
skip skipchars characters, or extract the substring starting from the position skipchars (the first character position is 0),
The length of the substring is length, this parameter can be ignored, which means Get all the remaining characters.
When this function appears on the left side of the equation, expr must be a variable or array element, and part of the substring is replaced by the value on the right side of the equation.
6.study
Syntax:
study(scalar);
Semantics:
Use an internal format to improve the access speed of variables. At the same time, only Acts on a variable.
7. lc, uc
Syntax:
retval=lc(string);
retval=uc(string);
Semantics:
Convert all strings to lower/uppercase letters.
8. lcfirst, ucfirst
Syntax:
retval=lcfirst(string);
retval=ucfirst(string);
Semantics:
Convert first letter to lower/upper case.
9. quotameta
Syntax:
newstring=quotemeta(oldstring);
Semantics:
Add a backslash ( \).
Statement: $string=quotemeta($string);
Equivalent to: $string=~s/(\W)/\\$1/g;
Commonly used in pattern matching operations , ensuring that no characters in the string are treated as match operators.
10. join
Syntax:
join(joinstr,list);
Semantics:
Combine string lists (arrays) into a long String, insert string joinstr between every two list elements.
11. sprintf
Syntax:
sprintf(string,fields);
Semantics:
Similar to printf, the difference is that the results are not output to a file. And as the return value is assigned to the variable.
Example$num=26;
$outstr=sprintf("%d=%x hexadecimal or %o octal\n",$num,$num,$num);
print($outstr);
The result output is 26=1a hexadecimal or 32 octal
The above is the detailed content of Summary of Perl's commonly used string processing functions. For more information, please follow other related articles on the PHP Chinese website!