Check If String Contains Substring in PowerShell [4 ways]

您所在的位置:网站首页 susbstring Check If String Contains Substring in PowerShell [4 ways]

Check If String Contains Substring in PowerShell [4 ways]

2023-04-20 13:39| 来源: 网络整理| 查看: 265

Check If String Contains Substring in PowerShell Case-Sensitive

In this section, we will see how to check if String contains substring Case-Sensitive.

Using -clike Operator

To check if string contains substring in PowerShell, use the -clike operator. -clike operator is type of match operator which is used to search for elements based on conditions using regex. It is used when you want to search for substring in String case-sensitive.

Using -clike operator 123456789  $string = "PowerShell"if ($string -clike "*SHE*"){    Write-Host "It is a substring of $string."}else{    Write-Host "It is not a substring of $string."} 

Output:

Output 123  It is not a substring of PowerShell. 

We assigned the string to the $string variable. Using the -clike operator and wildcard character *, we checked whether the word SHE matches the pattern with string in $string.

If the pattern is matched, the word SHE is a substring of the $string. If not, the $string does not contain the substring SHE.

Using Contains() Method

To check whether a string contains a substring, use Contains() Method. contains() method checks if String contains specified word or not.

Using contains() method 123456789  $string = "PowerShell"if ($string.Contains("she")){    Write-Host "It is a substring of $string."}else{    Write-Host "It is not a substring of $string."} 

Output:

Output 123  It is not a substring of PowerShell. 

The .NET class string.Contains() method prints True if the given character is found in the string. Otherwise, it returns False.

Further reading: Get Length of String in PowerShell Read more → Replace Space with Underscore in PowerShell Read more → Check If String Contains Substring in PowerShell Case-InSensitive

You may have noticed that the -clike operator and contains method are case sensitive. To perform a case-insensitive operation, use the following methods.

Using -like Operator

For the case-insensitive match, use -like instead of -clike.

Using -like operator 123456789  $string = "PowerShell"if ($string -clike "*she*"){    Write-Host "It is a substring of $string."}else{    Write-Host "It is not a substring of $string."} 

Output:

Output 123  It is a substring of PowerShell. 

As you can see, she is a substring of PowerShell as it is case insensitive search.

Using Contains() Method for Case Insensitive

By default, the Contains() method is case-sensitive. To use Contains() for a case-insensitive check, you can convert strings to the same case.

For instance, you can apply the ToLower() method to convert both strings to lowercase.

Using contains() method with ToLower() 123456789  $string = "PowerShell"if ($string.ToLower().Contains("she".ToLower())){    Write-Host "It is a substring of $string."}else{    Write-Host "It is not a substring of $string."} 

Output:

Output 123  It is a substring of PowerShell. 

The following example uses the ToUpper() method to convert both strings to uppercase.

Check if a string contains substring 123456789  $string = "PowerShell"if ($string.ToUpper().Contains("she".ToUpper())){    Write-Host "It is a substring of $string."}else{    Write-Host "It is not a substring of $string."} 

Output:

Output 123  It is a substring of PowerShell. 

That’s all about how to check if a string contains a word or substring in PowerShell. If you have any confusion, let us know in the comments.

Was this post helpful? Let us know if you liked the post. That’s the only way we can improve.


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3