本文主要是介绍《Learn Windows PowerShell in a Month of Lunches Third Edition》读书笔记—CHAPTER 25 Additional random tips,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
25.2 Operators: -as, -is, -replace, -join, -split, -in, -contains
25.2.1 -as and -is
The
-as
operator produces a new object in an attempt to convert an existing object into a different type.
如果我们有一个数包含小数部分,我们可以通过将该数转化为正数来删除小数部分:
1000 / 3 -as [int]
我们常用的类型有:[string], [xml], [int], [single], [double], [datetime]
is
运算符用来判断一个对象是不是某个类型的,返回True或False
123.45 -is [int]
"SERVER-R2" -is [string]
$True -is [bool]
(Get-Date) -is [datetime]
25.2.2 -replace
-replace
运算符用于在一个字符串中找到所有出现的字符串,并用第三个字符串替换这些出现的字符串
PS C:\> "192.168.34.12" -replace "34","15"
192.168.15.12
25.2.4 -contains and -in
The -contains operator is used to test whether a given object exists within a collection
PS C:\> $collection = 'abc','def','ghi','jkl'
PS C:\> $collection -contains 'abc'
True
PS C:\> $collection -contains 'xyz'
False
The -in operator does the same thing, but it flips the order of the operands so that the collection goes on the right, and the test object on the left:
PS C:\> $collection = 'abc','def','ghi','jkl'
PS C:\> 'abc' -in $collection
True
PS C:\> 'xyz' -in $collection
False
这篇关于《Learn Windows PowerShell in a Month of Lunches Third Edition》读书笔记—CHAPTER 25 Additional random tips的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!