Ackles
asked on
Powershell replace german characters in string
Hello,
I have an issue with user name, it can contain these characters:
ä, à, ö, é, ü, è & all these characters cause issues because we want to create user names with these.
I am looking for a way to replace these characters to
ä = ae
ö = oe
ü = ue
é = e
é =e
à =a
Please note that these are in a viable $Name, the issue is that any name can have either 1 or more than 1 of these characters.
So the solution should be that it should look for all these characters & if they exist should be replaced.
Thanks a lot in advance for help.
Regards,
A
I have an issue with user name, it can contain these characters:
ä, à, ö, é, ü, è & all these characters cause issues because we want to create user names with these.
I am looking for a way to replace these characters to
ä = ae
ö = oe
ü = ue
é = e
é =e
à =a
Please note that these are in a viable $Name, the issue is that any name can have either 1 or more than 1 of these characters.
So the solution should be that it should look for all these characters & if they exist should be replaced.
Thanks a lot in advance for help.
Regards,
A
ASKER
$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $correct.ToTitleCase($Firs tName.tolo wer())
$NameFirst = $FirstName -Replace ('ä', 'ae')
$NameFirst = $FirstName -Replace('ö', 'oe')
$LastName = Read-Host -Prompt 'LastName'
$LastName = $correct.ToTitleCase($Last Name.tolow er())
$DisplayName = $FirstName + " " + $LastName
$FirstName
$LastName
$UserName = ($NameFirst + "." + $LastName).ToLower() + "@test.com"
$UserName
This is my code & it doesn't work....
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $correct.ToTitleCase($Firs
$NameFirst = $FirstName -Replace ('ä', 'ae')
$NameFirst = $FirstName -Replace('ö', 'oe')
$LastName = Read-Host -Prompt 'LastName'
$LastName = $correct.ToTitleCase($Last
$DisplayName = $FirstName + " " + $LastName
$FirstName
$LastName
$UserName = ($NameFirst + "." + $LastName).ToLower() + "@test.com"
$UserName
This is my code & it doesn't work....
$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $correct.ToTitleCase($FirstName.tolower())
$FirstName = $FirstName -Replace ("ä", "ae")
$FirstName = $FirstName -Replace ("ö", "oe")
$LastName = Read-Host -Prompt 'LastName'
$LastName = $correct.ToTitleCase($LastName.tolower())
$LastName = $LastName -Replace ("ä", "ae")
$LastName = $LastName -Replace ("ö", "oe")
$DisplayName = $FirstName + " " + $LastName
$FirstName
$LastName
$UserName = ($FirstName + "." + $LastName).ToLower() + "@test.com"
$UserName
Not elegant but works
Note: this needs to be saved as Unicode, not ASCII or UTF8!
You might have to re-enter the special characters, don't know if they'll survive the transfer via Website.
You might have to re-enter the special characters, don't know if they'll survive the transfer via Website.
$ReplaceMap = New-Object -TypeName System.Collections.Hashtable
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'
$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($FirstName.ToLower())
$ReplaceMap.Keys | % {$FirstName = $FirstName.Replace($_, $ReplaceMap[$_])}
$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($LastName.ToLower())
$ReplaceMap.Keys | % {$LastName = $LastName.Replace($_, $ReplaceMap[$_])}
$DisplayName = $FirstName + " " + $LastName
$UserName = ($FirstName + "." + $LastName).ToLower() + "@test.com"
$FirstName
$LastName
$UserName
Main difference is the double quotes around the letters in the replace Statement
Here is a generic way to replace single characters: http://poshcode.org/1054
So I'd first replace the German Umlauts and ß with their corresponding double characters and send the rest through Remove-Diacritics
Here is a generic way to replace single characters: http://poshcode.org/1054
So I'd first replace the German Umlauts and ß with their corresponding double characters and send the rest through Remove-Diacritics
ASKER
Dear oBdA,
It does work, however the FirstName & LastName should be as entered by the user only the $UserName should be with replaced string.
I guess the replaced values have to be passed to a new variable for both & then those variables would create username....
Also, is it possible to remove any space in between e.g. the user inputs Jack Bauer but the result should be Jackbauer?
Thanks a lot!
A
It does work, however the FirstName & LastName should be as entered by the user only the $UserName should be with replaced string.
I guess the replaced values have to be passed to a new variable for both & then those variables would create username....
Also, is it possible to remove any space in between e.g. the user inputs Jack Bauer but the result should be Jackbauer?
Thanks a lot!
A
ASKER
I passed it to variable & it works, however if the first character is the special character then it's skipped because it's capitalised.....
$ReplaceMap = New-Object -TypeName System.Collections.Hashtab le
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'
$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($Firs tName.ToLo wer())
$First = $FirstName
$ReplaceMap.Keys | % { $First = $First.Replace($_, $ReplaceMap[$_]) }
$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($Last Name.ToLow er())
$Last = $LastName
$ReplaceMap.Keys | % { $Last = $Last.Replace($_, $ReplaceMap[$_]) }
$DisplayName = $FirstName + " " + $LastName
$UserName = ($First + "." + $Last).ToLower() + "@test.com"
$FirstName
$LastName
$First
$Last
$UserName
$ReplaceMap = New-Object -TypeName System.Collections.Hashtab
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'
$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($Firs
$First = $FirstName
$ReplaceMap.Keys | % { $First = $First.Replace($_, $ReplaceMap[$_]) }
$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($Last
$Last = $LastName
$ReplaceMap.Keys | % { $Last = $Last.Replace($_, $ReplaceMap[$_]) }
$DisplayName = $FirstName + " " + $LastName
$UserName = ($First + "." + $Last).ToLower() + "@test.com"
$FirstName
$LastName
$First
$Last
$UserName
ASKER
I corrected it & is fine.
Can you please answer the removing of the space issue?
Can you please answer the removing of the space issue?
This will now replace only for the username, and remove the space (for the username only as well.
Works just fine here with a capital special character.
Works just fine here with a capital special character.
$ReplaceMap = New-Object -TypeName System.Collections.Hashtable
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['à'] = 'a'
$Correct = (Get-Culture).TextInfo
$FirstName = Read-Host -Prompt 'FirstName'
$FirstName = $Correct.ToTitleCase($FirstName.ToLower())
$LastName = Read-Host -Prompt 'LastName'
$LastName = $Correct.ToTitleCase($LastName.ToLower())
$DisplayName = $FirstName + " " + $LastName
$UserFirst = $FirstName.Replace(' ', '')
$ReplaceMap.Keys | % {$UserFirst = $UserFirst.Replace($_, $ReplaceMap[$_])}
$UserLast = $LastName.Replace(' ', '')
$ReplaceMap.Keys | % {$UserLast = $UserLast.Replace($_, $ReplaceMap[$_])}
$UserName = ($UserFirst + "." + $UserLast).ToLower() + "@test.com"
$FirstName
$LastName
$UserName
ASKER
I added this to Replacemap:
$ReplaceMap = New-Object -TypeName System.Collections.Hashtab le
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['É'] = 'e'
$ReplaceMap['È'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['À'] = 'a'
$ReplaceMap['à'] = 'a'
$ReplaceMap['À'] = 'a'
$ReplaceMap[' '] = ''
& seems to work or is there another way?
$ReplaceMap = New-Object -TypeName System.Collections.Hashtab
$ReplaceMap['Ä'] = 'Ae'
$ReplaceMap['Ö'] = 'Oe'
$ReplaceMap['Ü'] = 'Ue'
$ReplaceMap['ä'] = 'ae'
$ReplaceMap['ö'] = 'oe'
$ReplaceMap['ü'] = 'ue'
$ReplaceMap['ß'] = 'ss'
$ReplaceMap['é'] = 'e'
$ReplaceMap['É'] = 'e'
$ReplaceMap['È'] = 'e'
$ReplaceMap['è'] = 'e'
$ReplaceMap['á'] = 'a'
$ReplaceMap['À'] = 'a'
$ReplaceMap['à'] = 'a'
$ReplaceMap['À'] = 'a'
$ReplaceMap[' '] = ''
& seems to work or is there another way?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome!!!
Open in new window