Perl String Processing Memo _perl

Source: Internet
Author: User
Tags chop numeric


1. Chop and Chomp functions

All two functions take a string as a parameter and remove the last character at the end. The difference is that, regardless of the last character, chop is stripped and returned in the return value, and Chomp is stripped only when the last character of the string is the same as the character $/the special variable. By default, $/saves newline characters and can, of course, be reset.
1 if the function argument is an array of strings, then the function is called for each element in the array.
2 If the function argument is a conforming list (that is, there are other lists or hash tables in the list), you can assume that the address (pointer) of each composite container is stored in the argument list, and that at this point chop will treat the address as a string, destroying the entire compound list.
Incidentally, if a variable holds a numeric type, rather than a string, the function interprets the value of the variable as a string.



2. getc function

The GETC function has only one parameter, which is the file handle, and if you want to read from the standard input stream, use stdin. This function returns the bytes read, and note that if you read a file stream, the function returns an empty string at EOF, that is, if you read the standard input stream, it blocks.



3. Stitching string

Use the Join function to splice individual strings, signed as join (Delmiter, list). Where the delimiter is a delimited string, the separate strings are delimited by the delimiter in the concatenation result, and the list is a separate string, or it can be an array holding several strings.





 code as follows:

$str 1 = "Stringa"; $str 2 = "STRINGB";
$combine = Join ':: ', $str 1, $str 2; # $combine value is:: STRINGA::STRINGB
@list1 = ($str 1, $str 2); @list2 = QW (STRINGC stringd);
$combine = Join ' _ ', @list1, @list2, $STR 1; # $combine value is _stringa_stringb_stringc_stringd_stringa





When the delimiter is used "\ n", it can be spliced into a vertical list.



4. Split string

String segmentation using the Split function.
Split (delimiter, string, count)
Delimiter is a separator, the default is a space (tab, space, etc. to split)
String is a string that is split and is $_ when the parameter is not specified
The maximum number of copies allowed to be split, and when the number of copies actually divisible exceeds that value, the count string will hold the remaining strings
function returns an array of all the strings that are divided



5. Repeat concatenation string

The x operator in Perl, note that operands must be left to parse into strings, and the right can be parsed into numeric values.





code as follows:

$STR 1 = "abc";
$str 2 = $str 1 x 5; # The value of $str 2 is ' abcabcabcabcabc '





6. String replacement

SUBSTR (string, offset, length):
String: Original string, offset: Starting position in the original string of strings, length: string lengths
There are two ways to use this function:
1) Get substring


code as follows:

$str = "Abcdefghijk";
$sstr = substr ($str, 5, 3); # $sstr value is ' EFG '





2) Replace substring


 code as follows:

$str = "1234567890";
SUBSTR ($str, 4, 3) = "xxxxx"; # $str value is ' 123xxxxx7890 '





The third parameter of the function specifies the length of the substituted substring, so the length of the newly replaced string is not affected.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.