Perl String correlation function

The built-in functions for strings are :
chomp, chop, chr, crypt, fc, hex, index, lc, lcfirst, length, oct, ord, pack,
q//, qq//, reverse, rindex, sprintf, substr, tr///, uc, ucfirst, y///
There are several categories :

* Character case conversion class :
* lc:(lower case) Convert subsequent letters to lowercase , yes \L Implementation of

* lcfirst: Convert the first letter after it to lowercase , yes \l Implementation of

* uc:(uppercase) Convert subsequent letters to uppercase , yes \U Implementation of

* ucfirst: Convert the first letter after it to uppercase , yes \u Implementation of

* fc:(foldcase) and lc Basic equivalence , It's just fc Can handle UTF-8 The letter of the class

* Character processing function :
* chomp: Remove line breaks at the end of a line

* chop: Remove end of line characters , A detailed example is given later chop
<https://www.cnblogs.com/f-ck-need-u/p/9666341.html#blogchop>

* reverse: Reverse list , Scalar string ,hash, A detailed example is given later [reverse(#blogreverse)]

* substr: Get string , A detailed example is given later substr
<https://www.cnblogs.com/f-ck-need-u/p/9666341.html#blogsubstr>

* tr///: Character mapping , see tr/// <https://www.cnblogs.com/f-ck-need-u/p/9655841.html>

* y///: Equivalent to tr///

* Character position index :
* index: Gets the index position of the character , A detailed example is given later index and rindex
<https://www.cnblogs.com/f-ck-need-u/p/9666341.html#blogindex>

* rindex: It is also the index position where the character is located , A detailed example is given later index and rindex
<https://www.cnblogs.com/f-ck-need-u/p/9666341.html#blogindex>

* Base conversion class :
* hex: take 16 Convert base to decimal . When the character is 0x or x It is automatically considered a hexadecimal number at the beginning

* oct: take 8 Convert base to decimal . When the character is 0 It is automatically treated as 8 Base number

* ord: Converts the first character of a string to ascii code

* chr: take ascii Convert to the corresponding character

* Other categories :
* crypt:( For the time being )

* length: Returns the number of characters in a string , A detailed example is given later length
<https://www.cnblogs.com/f-ck-need-u/p/9666341.html#bloglength>

* pack:( For the time being )

* q//: It is equivalent to putting single quotation marks on a string (quote)

* qq//: This is equivalent to double quotation marks for a string (double quote)

* sprintf:( slightly )printf It's the same as publishing , Not for output , It is used to return the content of the output
<>

chop

and chomp It's kind of similar .

* chop Remove end of line characters , Returns the character removed at the end of a line
* modify hash When , Remove each value Last character of , Not the last one key, And return to the last one value Deleted characters
* When modifying the list , Remove the last character of each element
1. Modify string
$str="abnc"; print chop $str; # output c print $str; # output abn
2. modify hash
%myhash=(name => "malongshuai", prov => "jiangxi", sch => "linchuan" );
$choped = chop %myhash; while(($key,$value) = each %myhash){ print
$key,":","$value\n"; } print "=" x 9,"\n"; print "$choped";
Execution return :
prov:jiangx sch:linchua name:malongshua ========= i
3. Modify list
@list=qw(aaa bbb ccc ddd); chop @list; # return aa bb cc dd print "@list";
<>

reverse

reverse Used to reverse the list , Scalar string ,hash.

* When reversing the list , Will return a list in reverse order

* When placed in scalar context , Will do string inversion

* When reverse hash Time , Will put value Reverse to key, So when value When there are duplicate values , Some key values are discarded when reversing

* reverse It's not acting on the original content , Instead, the reverse result is returned by the return value
Reverse list : Invert elements
@arr=qw(abc def ghi); @arr=reverse @arr; print "@arr"; # output (ghi def abc) print
join(",",reverse "hello","world") ; # output :world,hello
Scalar context : Concatenate the elements to get a scalar , Then reverse the scalar , Even if the reversal target is a list
@arr=qw(aA bB cC dD ); print scalar reverse @arr; # output :DdCcBbAa print "\n";
print @arr; # output :aAbBcCdD
Reverse string :
print scalar reverse "hello"; # output olleh
reversal hash: Will be able to value Reverse to key, therefore value When repeated , Some key values will be discarded
%arr=qw(aA bB cC dD ); %arr1=reverse %arr; while(($key,$value)=each %arr1){
print "$key -> ","$value","\n"; }
results of enforcement :
dD -> cC bB -> aA
<>

substr

Used to extract a substring from a given string .

usage :
substr STRING,OFFSET,LENGTH,REPLACEMENT substr STRING,OFFSET,LENGTH substr
STRING,OFFSET
among :

* offset from 0 Start the calculation

* offset When it is negative , Represents the displacement from the tail ( from -1 Start the calculation ) Move forward

* length If ignored , From offset Start at the end

* length When it is negative ,length Is the displacement position from the back to the front , So we will extract from offset reach length Substring at

* replacement replace string The string extracted from the . Two things need to be noted :
* Yes replacement, The extracted substring will be returned
* But the source string STRING Has been replaced use 5.010; $str="love your everything"; say substr $str,5;
# output :your everything say substr $str,-10; # Take it from back to front :everything say substr
$str,5,4; # Pick up after you go 4 Characters :your say substr $str,5,-3; # From displacement 5 Take the displacement -3( From back to front ):your
everyth say substr $str,5,4,"fairy's"; # Replace source string , But return to extract substring :your say $str; #
The source string has been replaced :love fairy's everything
You can substr Function as lvalue (lvalue), This allows you to modify the source variable , It's like giving replacement The parameters are the same :
use 5.010; $str="love your everything"; substr($str,5,4) = "fairy's"; say
$str; # The source string has been replaced :love fairy's everything
<>

index and rindex

index and rindex Used to find the index position of a substring or a character in a given string .

usage :
(r)index STRING,SUBSTR,POSITION (r)index STRDING,SUBSTR
* index For searching STRING For the first time SUBSTR Location of ,rindex Search for the last occurrence SUBSTR position
* If omitted position, From the starting position ( from 0 Start the calculation ) Start searching for the first occurrence of a substring
* given position, From position Start search at , If it is rindex, It's looking for position sinister
* If STRING Not found in SUBSTR, Return to -1 use 5.010; $str="love you and your everything";
say index $str,"you"; # output :5 say index $str,"yours"; # output :-1 say index
$str,"you",136; # output :-1 say index $str,"you",6; # from offset=6 Search after , output :13 say
rindex $str,"you"; # output :13 say rindex $str,"you",10; #
find offset=10 The last one on the left you, output :5
<>

length

The number of characters used to return a string , Not bytes . If it's the number of bytes , Then the unicode modular .

In the following example , Will output 11.
$str="hello world"; print length $str;
length Cannot act directly on arrays and hash To count the number of elements . Want to count the number :

* For arrays , Put it directly in the scalar context

* about hash, Available keys Return function hash key , Then put in the scalar context # Number of array elements @arr=qw(aaa bbb ccc ddddd eee
ff); print scalar @arr; # output 6 # hash Number of elements %myhash=qw(aaa bbb ccc ddddd eee ff);
print scalar keys %myhash;
If length The object for is undefined , Return to undef. If length The object for has no characters but is defined , Return to 0.