Category Archives: Powershell

Count the occurrence of given character in a string

Last time I showed how to get the number of occurrences of each character in a string but how do you count the occurrence of given character in a string? You use one of the fundamental concepts on which PowerShell … Continue reading

Posted in Powershell | 3 Comments

Count occurrence of characters in a string

How do you count occurrence of characters in a string? Group-object is a ready made cmdlet to answer this question function measure-occurrence {   [CmdletBinding()]   param (     [string]$teststring   )      $teststring.ToCharArray() |   Group-Object -NoElement |   Sort-Object -Property Count -Descending } Split … Continue reading

Posted in Powershell | Leave a comment

Test if string contains numeric

How can you test if a string contains a numeric character? The simple answer is to use a regular expression.  If you’ve been reading my stuff for any length of time you’ll know how much I love regular expressions. This … Continue reading

Posted in Powershell | 1 Comment

Reverse a string

I sort of brushed over it on my last post but this is how you reverse a string. function get-reversestring {   [CmdletBinding()]   param (     [string]$teststring   )   $ca = $teststring.ToCharArray()   [array]::Reverse($ca)   -join $ca } Take the input string … Continue reading

Posted in Powershell | Leave a comment

Test if a string is a palindrome

This is the first in a short series in which I’ll look at some string handling techniques.  PowerShell is all about objects but sometimes you just have to work with the basics. In this post I’ll show how to test … Continue reading

Posted in Powershell | 4 Comments

Third way to find pairs for given sum

There’s a third way to find pairs for given sum that’s a bit more complicated. function get-pairs2 {   [CmdletBinding()]   param (     [int[]]$iarray,     [int]$value   )        $sarray = $iarray | Sort-Object   Write-Information -MessageData “Array: $iarray” -InformationAction Continue   Write-Information -MessageData … Continue reading

Posted in Powershell | Leave a comment

A more elegant way to find pairs

Last time I showed a brute force way to find the pairs of numbers in an array that would sum to a given value. This time I have a more elegant way to find pairs. function get-pairs1 {   [CmdletBinding()]   param … Continue reading

Posted in Powershell | Leave a comment