当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

作者:精品下载站 日期:2024-12-14 04:49:28 浏览:16 分类:玩电脑

PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例


[玩转系统] 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("-")

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

例子#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(".")

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

例子#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

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

例子#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 "**********"

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

例子#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]

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

例子#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

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

例子#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(".")

输出:

[玩转系统] PowerShell 拆分字符串 | PowerShell 拆分字符串的各种示例

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 拆分字符串 | PowerShell 拆分字符串的各种示例

结论

因此,本文详细介绍了 PowerShell 中的分割字符串方法。它用适当的例子解释了各种分隔符。它还展示了使用拆分操作生成子字符串的各种方法。它还简要解释了如何使用 split path cmdlet 从给定路径中拆分路径。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯