[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例
作者:精品下载站 日期:2024-12-14 04:49:28 浏览:16 分类:玩电脑
PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例
PowerShell 分割字符串简介
PowerShell使用Split()函数将一个字符串拆分成多个子字符串。该函数使用指定的分隔符将字符串拆分为子字符串。用于分割字符串的默认字符是空格。三种类型的元素与 split 函数相关。它们是分隔符、子字符串的最大数量以及与分隔符相关的选项,SimpleMatch 或 Multiline。
分隔符:默认分隔符是空格。也可以使用其他分隔符,例如模式、制表符和退格键。
子字符串的最大数量:默认情况下,该函数返回所有可能的子字符串。我们还可以使用此元素来限制它们。
语法:
.Split(strSeparator [, MaxSubstrings] [, Options])
String -Split strSeparator [, MaxSubstrings] [, Options]
String -Split {scriptblock} [, MaxSubstrings]
-Split String
参数:
1. -String[]或String:它表示要从实际输入中分割出来的字符串。也可以指定多个字符串。
2. -分隔符:这表示用于标识子字符串结尾的字符。默认分隔符是空格,包括制表符和换行符。默认情况下,所有子字符串中都会省略分隔符,要包含它们,必须将其括在括号内。
3. -Max-SubStrings:表示输入子串必须分割的次数,即应生成的子串的数量。默认情况下,会生成所有可能的子字符串。如果子字符串多于指定数量,则剩余的子字符串将附加到最终子字符串。允许使用非负值,零返回所有子字符串。
4. -ScriptBlock:表示分隔符的规则。它包含在大括号内,并且必须评估为 true 或 false。
5. -选项:单行仅标识字符串的开头和结尾,多行标识行的开头和结尾以及字符串。
选项的语法如下,仅当使用 Max-SubStrings 参数时才能使用。
"SimpleMatch [,IgnoreCase]"
"[RegexMatch] [,IgnoreCase] [,CultureInvariant]
[,IgnorePatternWhitespace] [,ExplicitCapture]
[,Singleline | ,Multiline]"
除了 SingleLine 和 MultiLine 之外,可能的正则表达式选项如下:
- RegexMatch:这是默认选项。它使用正则表达式来计算分隔符。
- IgnoreCase:不考虑分隔符的大小写
- CultureInvariant:评估分隔符时会忽略语言中的文化差异。
PowerShell 拆分字符串的示例
下面给出了 PowerShell 拆分字符串的示例:
例子#1
输入:
Write-Host "generating substring using split method"
$teststring="my name is vignesh- am from chennai"
Write-Host "splitting using - character"
$teststring -split "-"
$teststring="domainname\username"
Write-Host "Splitting using \ character"
$teststring -split "\"
Write-Host "generating substring using space"
$string="string1 string2 strin3"
$string.Split("")
Write-Host "splitting using multiple separators"
$string="domain\systems-test"
$string.Split("\").Split("-")
输出:
例子#2
输入:
Write-Host "Welcome to the Split string demo"
Write-Host "Splitting using white space"
$string="My name is vignesh Krishnakumar"
$string.Split("")
Write-Host "Splitting the string using single delimiter"
$teststring="there was, a man, 100 years ago, who used to, kill thousands of people, on a regualr basis, for no reason"
write-host "The input is" $teststring
Write-Host "The above input will be split using , as below"
$teststring.Split(",")
Write-Host "splitting the string using multiple delimiters"
$teststring1="there was, a man, whose name was king rama. he was a good person. his wife, name was sita."
Write-Host "input string is" $teststring1
Write-Host "splitting the above input using , and ."
$teststring1.Split(",").Split(".")
输出:
例子#3
输入:
Write-Host "Welcome to the Split string demo"
Write-Host "Usage of Max Substrings"
$string= "Jan-Feb-Mar-Apr-May-June-July-Aug-Sep-Oct-Nov-Dec"
Write-Host "Input string is" $string
Write-Host " Splititng the string to max 4 substrinngs"
$string -split "-" , 4
Write-Host "including the delimiter character is substring"
$string -split "(-)" , 4
输出:
例子#4
输入:
Write-Host "Welcome to the Split string demo"
Write-Host "Welcome to Regex tutorial"
$month="Jan-Feb-Mar-Apr-May-June-July-Aug-Sep-Oct-Nov-Dec"
Write-Host "Delimiter is n"
$month -split {$_ -eq "n"}
write-host "************"
Write-Host "Multiple regex expressions"
$month -split {($_ -eq "n") -or ($_ -eq "a")}
write-host "************"
Write-Host "Along with a numerical condition"
$test=5
$month -split {if ($test -gt 4) {$_ -eq "a"} else {$_ -eq "f"}}
write-host "************"
$month -split {if ($test -eq 4) {$_ -eq "z"} else {$_ -eq "f"}}
Write-Host "*********"
$month.Split("[nf]")
Write-Host "**********"
输出:
例子#5
输入:
Write-Host "Welcome to the Split string demo"
Write-Host "Extracting only particular substring"
$teststring= "hello how are you am fine my name is vignesh krishnakumar am from chennai"
$months=$teststring.Split(" ")
Write-Host "First Word is" $months[0]
Write-Host "third word is" $months[2]
Write-Host "Seventh Word is" $months[6]
输出:
例子#6
输入:
Write-Host "Welcome to the Split string demo"
Write-Host "*****************"
$input="sdfsd12323fgds567cxvdsfd12332"
Write-Host "The input is " $input
Write-Host "*****************"
Write-Host "Extracting numbers only from a string"
Write-Host "*****************"
$numbers= $input -split "\D"
Write-Host "extarcted numbers is " $numbers
Write-Host "*****************"
Write-Host "Extracting text only from a string"
Write-Host "*****************"
$numbers1= $input -split "\d"
Write-Host "extracted text is" $numbers1
输出:
例子#7
输入:
此示例将展示如何基于正则表达式拆分和生成子字符串以及如何拆分 MAC 地址。
Write-Host "split using regex"
$test=" this . is an . example . of an was an and an . . . ."
$test.Split("an")
Write-Host "splitting a mac address"
$test="12-23-AB-DE-45-BC"
$test.Split("-")
Write-Host "Splitting an IP Address"
$test="122.43.56.78"
$test.Split(".")
输出:
Split-Path
分割路径用于返回路径中提到的部分。它可以是父文件夹、文件名或子文件夹。
语法:
AME
Split-Path
Split-Path [-Path] <string[]> [-Parent] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
Split-Path [-Path] <string[]> [-NoQualifier] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
Split-Path [-Path] <string[]> [-Leaf] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
Split-Path [-Path] <string[]> [[-Qualifier]] [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
示例:
输入:
Write-Host "Split Path example"
Write-Host "returning the drive of a path"
Split-Path -Path "C:\Users\R003646\Desktop\Articles\Jan" -Qualifier
Write-Host "Dispalying the files inside a folder"
Split-Path -Path "C:\Vignesh\Test\Test6\*.txt" -Leaf -Resolve
输出:
结论
因此,本文详细介绍了 PowerShell 中的分割字符串方法。它用适当的例子解释了各种分隔符。它还展示了使用拆分操作生成子字符串的各种方法。它还简要解释了如何使用 split path cmdlet 从给定路径中拆分路径。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag