网站搜索

如何在 PowerShell 中查找子字符串的位置


使用 PowerShell 中的 Indexof() 或 LastIndexOf() 方法,我们可以在 PowerShell 中查找子字符串的索引位置。

IndexOf() 方法查找指定字符串在 PowerShell 字符串中第一次出现的位置。IndexOf 方法从零开始计算索引。

LastIndexOf() 方法查找 PowerShell 字符串中指定字符串的最后一次出现。

在此示例中,我们将讨论如何使用 IndexOf 和 LastIndexOf 方法查找 PowerShell 子字符串 Index。

使用 PowerShell 中的 IndexOf 方法查找子字符串索引

使用 PowerShell 中的 IndexOf 方法查找给定 PowerShell 字符串中子字符串的索引。它从 0 开始计算索引。

在以下示例中,变量 $str 存储字符串数据。

为了查找子字符串索引位置,它使用 IndexOf() 方法并将子字符串传递给它。

$str.IndexOf("string")
$str = "PowerShell string IndexOf method finds first occurrence of substring in string" 

#Print the string
$str

# Find Substring Index Position
$str.IndexOf("string")

在上面的 PowerShell 脚本中,IndexOf 方法查找子字符串 string 的第一次出现,并获取指定子字符串的子字符串索引位置,即 11,这是 string 的起始位置。

上述脚本的输出是:

PS C:\> $str = "PowerShell string IndexOf method finds first occurrence of substring in string"                         
PS C:\> $str                                                                                                            
PowerShell string IndexOf method finds first occurrence of substring in string

PS C:\> $str.IndexOf("string")                                                                                          
11

IndexOf 方法具有重载方法,您可以在其中指定起始索引,以便忽略起始索引之前的字符串或字符。它查找起始索引之后第一次出现的字符串。

假设我们有一个如下所示的字符串

PowerShell string IndexOf method finds first occurrence of substring in string

要查找索引 15 之后的子字符串 string,请使用以下代码。

$str = "PowerShell string IndexOf method finds first occurrence of substring in string" 
$str.IndexOf("string",15)    

在上面的 PowerShell 脚本中,IndexOf 方法使用 startIndex 15 忽略起始索引之前出现的任何字符串,并开始搜索索引位置 15 之后的子字符串。

它返回给定字符串中子字符串的位置,如下所示。

在 PowerShell 中使用 LastIndexOf 查找子字符串的位置

使用 LastIndexOf 方法查找指定 PowerShell 字符串中的子字符串索引位置。它查找给定字符串中最后一次出现的字符串。

在以下示例中,$str 变量包含字符串数据。

获取子字符串最后一次出现的位置 multiple,参考下面的代码。

$str = "String Split() breaks the string into multiple parts"      
$str.LastIndexOf("multiple")     

在上面的 PowerShell 脚本中,LastIndexOf 方法采用输入参数来查找给定字符串中子字符串的最后一次出现。

上述脚本的输出是:

PS C:\> $str = "String Split() breaks the string into multiple parts"                                                   

PS C:\>                                                                                                                 
PS C:\> $str                                                                                                            
String Split() breaks the string into multiple parts

PS C:\>                                                                                                                 
PS C:\> $str.LastIndexOf("multiple")                                                                                    
38
PS C:\>                                                                                                                                                                                                                                                       

结论

我希望上面关于如何使用 IndexOf 和 LastIndexOf 方法查找给定字符串中子字符串的位置的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

相关文章