Table of Contents
Using the -replace Operator
To count the occurrences of a character in a String in PowerShell:
- Use the
-replaceoperator to replace the occurrences of the specified character with an empty string and store the result in a temporary string. - Use the
.Lengthproperty to get the length of the original and the temporary string. - Subtract the length of the temporary string from the original string to get the character count.
|
1 2 3 4 5 6 7 |
$string = "my_string" $character = 'm' $temp_string = ($string -replace $character,"") $count = $string.Length - $temp_string.Length Write-Output "Occurence frequency of '$character' in '$string' is $count" |
|
1 2 3 |
Occurence frequency of 'm' in 'my_string' is 1 |
The -replace operator in PowerShell replaces one or more specified characters or strings with another string. It returns a new string without modifying the original string. It takes two arguments:
- The first is the value we want to replace.
- The second is the value we want to replace the first with.
We can also remove specific characters from a string using the -replace operator to replace particular characters with an empty string. For example, we used this operator to remove the occurrences of the character from the string by replacing them with empty strings. We stored the result of the replacement operation in a temp_string.
In PowerShell, System.String class in the .NET Framework represents a sequence of Unicode characters. It provides various methods and properties for working with strings. For example, its Length property returns the number of characters in a string.
The .Length property combines with the -replace operator to determine how many times a specific character appears in a string. For example, we used this property to count the number of characters in the string and temp_String. The temp_string contains characters other than the character. Therefore, we subtracted the length of the temp_string from the string to get the occurrence frequency of the character in the string.
Using the .Split() Method
To get the occurrence frequency of a character in a String in PowerShell:
- Use the
.Split()method to split thestringinto an array of substrings specifying thecharacteras the delimiter. - Use the
.Countproperty to get the number of elements in the array, which will be the number of occurrences of thecharacterin the original string.
|
1 2 3 4 5 6 |
$string = "my_string" $character = 'm' $count = ($string.Split($character)).count-1 Write-Output "Occurence frequency of '$character' in '$string' is $count" |
|
1 2 3 |
Occurence frequency of 'm' in 'my_string' is 1 |
The .Split() method of the System.String class splits a string into an array of substrings according to the given delimiter. The delimiter is a character or a set of characters that separate the substrings. The resulting array will contain all the substrings that were in the original string but not the delimiter. For example, we applied the .Split() method on the string to split it by considering the character as a delimiter.
However, the .Count property is a property of the System.Collections.ICollection interface returns the number of elements in a collection. Therefore, it can get the number of elements in an array. For example, we used this property to get the number of elements in the resultant array.
Note that the count returned is
1less than the total number of occurrences. It is because the count returns the number of elements in the array created by splitting the original string that does not include the delimiter. So we subtract1from the output to get the exact occurrence frequency.
Using the .ToCharArray() Method
To get the count of occurrences of a character in a String in PowerShell:
- Use the
.ToCharArray()method to convert thestringinto an array of characters. - Use the
Where-Objectcmdlet to filter out only the elements of the array that are equal to thecharacter. - Use the
.Countproperty to count the number of elements in the filtered array.
|
1 2 3 4 5 6 |
$string = "my_string" $character = 'm' $count = ($string.ToCharArray() | Where-Object {$_ -eq $character}).count Write-Output "Occurrence frequency of '$character' in '$string' is $count" |
|
1 2 3 |
Occurrence frequency of 'm' in 'my_string' is 1 |
System.String class’s .ToCharArray() method converts an object in PowerShell to a one-dimensional array of characters. The resulting array contains one element for each character in the original string. We can use this method to perform operations on individual characters of a string or to manipulate the string as an array of characters. For example, we used this method to convert the string to a character array.
The pipe (|) operator passes the output of one command or cmdlet as the input to another command or cmdlet. We used the pipe operator to process the output of the .ToCharArray() method.
The conditional cmdlet Where-Object filters a collection of objects in PowerShell. We filtered the output of the .ToCharArray() method where the element equals the specified character using the -eq operator.
We discussed the .Count property while explaining the code section for using the .Split() method. In this section, we used this property to count the number of elements in the filtered array, which will be the number of occurrences of the character in the original string.
Further reading:
Using the [regex]::matches() Method
To calculate the occurrence frequency of a character in a String in PowerShell, use the [regex]::matches() method with the .Count parameter.
|
1 2 3 4 5 6 |
$string = "my_string" $character = 'm' $count = [regex]::matches($string, $character).count Write-Output "Occurence frequency of '$character' in '$string' is $count" |
|
1 2 3 |
Occurence frequency of 'm' in 'my_string' is 1 |
In PowerShell, [regex]::matches() is a static method of the System.Text.RegularExpressions.Regex class that finds all the matches of an expression in a given input string.
The [regex]::matches() method takes two arguments:
- The first argument is an expression pattern or character we want to match.
- The second argument is the input string, where we want to find the matches.
For example, we used this method to search for the character occurrences in the string. On the output of this method, we used the .Count parameter to calculate the occurrence frequency.
Note that this method is less efficient than the others we previously discussed, as it returns a
System.Text.RegularExpressions.MatchCollectionobject, which contains oneSystem.Text.RegularExpressions.Matchobject for each match found while the others use string operations.
That’s all about how to count occurrences of character in String in PowerShell.