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:
If I want to replace the "BC" in the string "ABCD" with null, the code is as follows:
Command:
Copy Code code as follows:
Output:
Copy Code code as follows:
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:
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:
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?