Common operation of variable quantum strings

Source: Internet
Author: User

Common operation of variable quantum strings

${#string} Returns the length of the $string

${#string:p Osition} in $string, start extracting substrings from position $position

${#string:p Osition:length} in $string, extract the substring of length $length starting from the position $position

${string#substring} deletes the shortest match $substring substring starting at the beginning of the variable $string

${string# #substring} deletes the longest matching $substring substring starting at the beginning of the variable $string

${string%substring} deletes the shortest match $substring substring starting at the beginning of the variable $string open tail

${string%%substring} deletes the longest matching $substring substring starting at the beginning of the variable $string open tail

${string/ubstring/replace} from the variable $replace to replace the first matching $substring

${string/#substring/replace} if the $string prefix matches $substring, $replace is used instead of matching $substring

${string/%substring/replace} If the $string suffix matches $substring, use $replace instead of matching $substring

1. Returns the length of string Caimz variable string

[Email protected] shell_scrpit]# caimz= "I am a Boy"
[Email protected] shell_scrpit]# echo $caimz
I am a Boy
[Email protected] shell_scrpit]# echo ${#caimz} #查字符串的个数
10

[Email protected] shell_scrpit]# echo $caimz |wc-m
11

2, intercept Caimz variable string from the second character after the interception, the default to go back to all the characters, the second string is not included, also

Can be understood as deleting the preceding characters.

[[email protected] shell_scrpit]# echo ${caimz:2}
Am a Boy

3, intercept the CAIMZ variable string from the second string to start fetching, go to two characters

[[email protected] shell_scrpit]# echo ${caimz}
I am a Boy
[[email protected] shell_scrpit]# echo ${caimz:2:2}
Am

Or

[[email protected] shell_scrpit]# echo ${caimz} |cut-c 3-4
Am

4, starting from the beginning of the variable $caimz delete the shortest match "I am" substring

[[email protected] shell_scrpit]# echo ${caimz#i am}
A boy

5. Delete the longest match "I am a" substring starting from the beginning of the variable $caimz

[[email protected] shell_scrpit]# echo ${caimz# #I am A}
Boy

6. Delete the shortest matching boy string starting from the end of the variable $caimz

[[email protected] shell_scrpit]# echo ${caimz%boy}
I am A

7, starting from the end of the variable $caimz delete the shortest length matching boy string

[[email protected] shell_scrpit]# echo ${caimz%%boy}
I am A

8, use you is string to replace $caimz first match I am string

[[email protected] shell_scrpit]# echo ${caimz/boy/man}
I am a man

9. Use the man string to replace the boy string that starts at the end of the variable $caimz

[[email protected] shell_scrpit]# echo ${caimz/%boy/dog}
I am a man

10. Use the He is string instead of the I-AM string that starts at the beginning of the variable $caimz

[Email protected] shell_scrpit]# echo ${caimz/i am/you is}
You is a boy


Common operation of variable quantum strings

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.