How to resolve the algorithm Strip control codes and extended characters from a string step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Strip control codes and extended characters from a string step by step in the PowerShell programming language

Table of Contents

Problem Statement

Strip control codes and extended characters from a string.

The solution should demonstrate how to achieve each of the following results:

In ASCII, the control codes have decimal codes 0 through to 31 and 127. On an ASCII based system, if the control codes are stripped, the resultant string would have all of its characters within the range of 32 to 126 decimal on the ASCII table. On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Strip control codes and extended characters from a string step by step in the PowerShell programming language

Source code in the powershell programming language

function Remove-Character
{
    [CmdletBinding(DefaultParameterSetName="Control and Extended")]
    [OutputType([string])]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]
        $String,

        [Parameter(ParameterSetName="Control")]
        [switch]
        $Control,

        [Parameter(ParameterSetName="Extended")]
        [switch]
        $Extended
    )

    Begin
    {
        filter Remove-ControlCharacter
        {
            $_.ToCharArray() | ForEach-Object -Begin {$out = ""} -Process {if (-not [Char]::IsControl($_)) {$out += $_ }} -End {$out}
        }

        filter Remove-ExtendedCharacter
        {
            $_.ToCharArray() | ForEach-Object -Begin {$out = ""} -Process {if ([int]$_ -lt 127) {$out += $_ }} -End {$out}
        }
    }
    Process
    {
        foreach ($s in $String)
        {
            switch ($PSCmdlet.ParameterSetName)
            {
                "Control"  {$s | Remove-ControlCharacter}
                "Extended" {$s | Remove-ExtendedCharacter}
                Default    {$s | Remove-ExtendedCharacter | Remove-ControlCharacter}
            }
        }
    }
}


$test = "$([char]9)Français."

"Original string              : `"$test`""
"Control characters stripped  : `"$($test | Remove-Character -Control)`""
"Extended characters stripped : `"$($test | Remove-Character -Extended)`""
"Control & extended stripped  : `"$($test | Remove-Character)`""


"Français", "Čeština" | Remove-Character -Extended


  

You may also check:How to resolve the algorithm Comments step by step in the Gri programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Go programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Sed programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the Prolog programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the Ruby programming language