PowerShell replacement string operator Replace concise tutorial _powershell

Source: Internet
Author: User

PowerShell's handling of strings has a very powerful function, stronger than any scripting language. Let's take a look at the replacement string operation today.

If I want to replace "a" in the string "ABCD" with "X", the code is as follows:

Command:

Copy Code code as follows:
PS > "ABCD"-replace "a", "X"

Output:
Copy Code code as follows:
Xbcd

If I want to replace the "BC" in the string "ABCD" with null, the code is as follows:

Command:

Copy Code code as follows:

PS > "ABCD"-replace "BC"

Output:
Copy Code code as follows:
Ad

There are two more simple replacements, and the following plays a regular expression replacement:

If I want to replace the previous character "a" in the string "AAABCDE" with null, the code is as follows:

Command:

Copy Code code as follows:
PS > "AAABCDE"-replace "^a*"

Output:
Copy Code code as follows:
Bcde

One more, if I want to replace the string "DFAQ-ADFDFSAFD-ASDFADF" with a "xxx" between two bars, the code is as follows:

Command:

Copy Code code as follows:
PS > "DFAQ-ADFDFSAFD-ASDFADF"-replace "-.*-", "-xxx-"

Output:
Copy Code code as follows:
Dfaq-xxx-asdfadf

Well, about how PowerShell uses the-replace operator to replace strings, so much for the small series. Xiao Bian thinks example is the best way to learn, do you think?

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.