《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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值