Perl string processing Memorandum

Source: Internet
Author: User
Tags chop

1. Chop and chomp Functions

Both functions use strings as parameters and remove the last character from them. The difference is that, no matter what the last character is, chop will be removed and returned in the return value. Only when the last character of the string is the same as the character saved in the special variable $, chomp will remove it. By default, $/saves the newline character. You can reset it.
1) if the function parameter is a string array, the function calls each element in the array.
2) If the function parameter is a compliant list (that is, there are other lists or hash tables in the list), it can be considered that the address (pointer) of each composite container is saved in the parameter list ), at this time, chop will process the address as a string, thus damaging the entire composite list.
By the way, if a variable saves the value type rather than a string, the function interprets the value saved in the variable as a string.

2. GETC Function

The GETC function has only one parameter, that is, the file handle. to read data from a standard input stream, stdin is used. This function returns the read bytes. Note that if a file stream is read, the function returns an empty string ('') at the EOF. If a standard input stream is read, it will always be blocked.

3. concatenate strings

The join function can be used to concatenate various strings and the signature is join (delmiter, list ). Delimiter is a separator string. In the splicing result, each separator string is separated by the delimiter. List is a separate string, or an array that stores several strings.

CopyCode The Code is as follows: $ str1 = "stringa"; $ str2 = "stringb ";
$ Combine = join ':', $ str1, $ str2; # $ combine value: stringa: stringb
@ List1 = ($ str1, $ str2); @ list2 = QW (stringc stringd );
$ Combine = join '_', @ list1, @ list2, $ str1; # $ combine value: _ stringa_stringb_stringc_stringd_stringa

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

4. Split the string

Use the split function to separate strings.
Split (delimiter, String, count)
Delimiter is the delimiter. The default Delimiter is space (separated by tab and space)
String is the string to be split. If this parameter is not specified, $ _ is split.
Count indicates the maximum number of parts that can be split. when the actual number of parts that can be split exceeds this value, the Count string stores the remaining strings.
The function returns an array containing all split strings.

5. Repeat the concatenated string

The X operator in Perl. Note that the left side of the operand must be parsed into a string, and the right side can be parsed into a number.

Copy code The Code is as follows: $ str1 = "ABC ";
$ Str2 = $ str1 x 5; # the value of $ str2 is "abcabcabcabcabc"

6. String replacement

Substr (string, offset, length ):
String: original string, offset: Start position of the original string where the string is located, Length: String Length
This function can be used in two ways:
1) obtain the substring

Copy code The Code is as follows: $ STR = "abcdefghijk ";
$ SSTR = substr ($ STR, 5, 3); # the value of $ SSTR is "EFG"

2) Replace substrings

Copy code The Code is as follows: $ STR = "1234567890 ";
Substr ($ STR, 4, 3) = "XXXXX"; # $ STR value: "123xxxxx7890"

The third parameter of the function specifies the length of the substring to be replaced, so the length of the new 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.