- PowerShell功能特点
- PowerShell历史
- PowerShell和命令提示符的区别
- PowerShell与Bash Shell的区别
- PowerShell以管理员身份运行
- Windows PowerShell ISE
- PowerShell核心
- 创建并运行PowerShell脚本
- PowerShell注释
- PowerShell Cmdlet
- PowerShell基本cmdlet命令
- PowerShell Get-childItem命令
- PowerShell Get-Item命令
- PowerShell Get-Location命令
- PowerShell set-item命令
- PowerShell set-location命令
- PowerShell new-item命令
- PowerShell copy-item命令
- PowerShell move-item命令
- PowerShell remove-item命令
- PowerShell rename-item命令
- PowerShell add-content命令
- PowerShell clear-content
- PowerShell get-content命令
- PowerShell get-date命令
- PowerShell set-content命令
- PowerShell out-file命令
- PowerShell write-host命令
- PowerShell get-command命令
- PowerShell invoke-command命令
- PowerShell get-help命令
- PowerShell start-process命令
- PowerShell test-path命令
- PowerShell foreach-object命令
- PowerShell sort-object命令
- PowerShell where-object命令
- PowerShell变量
- PowerShell自动变量
- PowerShell首选项变量
- PowerShell数组
- PowerShell哈希表
- PowerShell运算符
- PowerShell算术运算符
- PowerShell赋值运算符
- PowerShell比较运算符
- PowerShell逻辑运算符
- PowerShell重定向运算符
- PowerShell拆分和合并运算符
- PowerShell if语句
- PowerShell if-else语句
- PowerShell else-if语句
- PowerShell Switch语句
- PowerShell Do-While循环
- PowerShell for循环
- PowerShell ForEach循环
- PowerShell While循环
- PowerShell Continue和Break
- PowerShell字符串
- PowerShell函数
- PowerShell Try Catch Finally
PowerShell字符串
PowerShell字符串是具有System.String
类型的对象。它是一种表示字符序列的数据类型,可以是文字常量或某种变量。
可以在PowerShell中使用单引号或双引号来定义字符串。这两个字符串都是使用相同的System.String
对象类型创建的。
1.示例
示例1:此示例描述如何在字符串中使用单引号:
PS C:\>$String1='It is a Single Quoted String' PS C:\>$String1 It is a Single Quoted String
示例2:此示例描述如何在字符串中使用双引号:
PS C:\> $String2="It is a double Quoted String" PS C:\> $String2 It is a double Quoted String
2.串联
字符串的连接使用加号执行。
示例1: 下面的示例描述如何连接两个字符串变量:
PS C:\> $s1="XN" PS C:\> $s2="TUTOR" PS C:\> $s1+$s2
上例中最后一条命令的输出将显示为:XNTUTOR
示例2:也可以使用join运算符来连接字符串。下面的示例描述如何使用此运算符:
PS C:\> $s1,$s2 -join ".COM"
本示例的输出将显示为:XN.COMTUTOR
示例3:也可以使用方法concat()
来连接字符串。下面的示例描述如何使用此方法:
$s1="Power" PS C:\> $s2="Shell" PS C:\> [System.String]::Concat($s1,$s2)
此示例的输出也将显示为:PowerShell
3.子串
SubString
是一种方法,它接受两个重载参数并返回较长字符串的一部分。 这两个参数都是数值,并用逗号(,
)分隔。第一参数是开始值,第二个表示想从开始位置向往右的字符数量。
示例:下面的示例跳过前三个字符,并从给定的字符串返回下一个七个字符。
PS C:\> $s1="Windows PowerShell" PS C:\> $s1.SubString(3,7)
上面示例中的第二个命令显示以下输出:
dows Po
4.字符串格式
字符串格式化是在字符串中插入一些字符或字符串的过程。 我们可以使用-f
运算符来格式化字符串。
$s1="Windows PowerShell" $s2="XNTUTOR" $formattedString = "{0} {1}...." -f $s1,$s2 $formattedString
执行上面示例代码,得到以下结果:
Windows PowerShell XNTUTOR....
5.字符替换
replace()
方法接受两个参数,并用于替换字符串中的字符。
示例:在下面的示例中,我们将给定字符串中的字符P
替换为S
。
PS C:\> $s1="Windows Powerxhell" PS C:\> $s1.replace("P","S")
上面示例中的第二个命令显示以下输出:
indows Sowerxhell
![程序员编程王-随时随地学编程](http://www.weizhi.cc/images/minapp/weixin_minapp12.jpg)
扫描二维码
程序员编程王