1

I have a function that replaces umlauts. If that function is saved as normal script file (.ps1), the output is test-aeoeueAeOeUe1234. This is what I expect to get :-)

function TestReplace{  
    Param(
        [parameter(Mandatory=$true,Position=0)][ValidateNotNullOrEmpty()][String]$InputString
    )

    $ResultString = ($InputString.replace('ä', 'ae').replace('ö','oe').replace('ü','ue').replace('Ä', 'Ae').replace('Ö','Oe').replace('Ü','Ue'))

    $ResultString
}

TestReplace -InputString "test-äöüÄÖÜ1234"

But if the same function is saved as part of a module (.psm1), the result is test-aeoeueaeoeue1234 - it seems the replace function is case insensitive.

I can't figure out, why the same code leads to different output...

  • I can't reproduce this behaviour. The code gives the same result for me when run in a simple script or as an imported module. Is there anything else your real code does that this sample doesn't show? – boxdog Jun 18 '19 at 09:36
  • When dealing with characters like this, it might be better to convert the string to a character array and deal with integer character codes instead, the string interpretation may vary depending on the host. Check out [this answer](https://stackoverflow.com/a/1271695/824495) in C#. – mehmetseckin Jun 18 '19 at 10:07

1 Answers1

0

When dealing with characters like this, it might be better to convert the string to a character array and deal with integer character codes instead, the string interpretation may vary depending on the host. Check out this answer in C#.

I have ported that solution to a PowerShell function, hope this helps:

function Replace-Diacritics {
  param 
  (
    $InputString
  )

  $dictionary = @{
    228 = "ae" # [int][char]'ä'
    246 = "oe" # [int][char]'ö'
    252 = "ue" # [int][char]'ü'
    196 = "Ae" # [int][char]'Ä'
    214 = "Oe" # [int][char]'Ö'
    220 = "Ue" # [int][char]'Ü'
    223 = "ss" # [int][char]'ß'
  }

  $sb = New-Object -TypeName "System.Text.StringBuilder";
  $null = ($InputString.ToCharArray() | % { if($dictionary[[int]$_]) { $sb.Append($dictionary[[int]$_]) } else { $sb.Append($_) } });
  return $sb.ToString();
}


$input = "test-äöüÄÖÜ1234";
$expected = "test-aeoeueAeOeUe1234";   
$result = Replace-Diacritics $input;
if($result -eq $expected) 
{
  Write-Host "Test passed. Expected: $expected, Actual: $result" -ForegroundColor Green
}
else
{
  Write-Host "Test failed. Expected: $expected, Actual: $result" -ForegroundColor Red
}
mehmetseckin
  • 3,061
  • 1
  • 31
  • 43
  • 1
    I think there is a typo in there: `$null = ($expected.ToCharArray()`. Shouldn't that be `$null = ($InputString.ToCharArray()` ? – Theo Jun 18 '19 at 19:30