Function Get-Acronym() {
[CmdletBinding()]
Param (
[string]$Phrase
)
( $Phrase -replace "[-_]"," " -split " " | Foreach-Object {$_[0].ToString().ToUpper()} ) -join ""
}
Get-Date cmdlet.
6th Nov 2024
·
Found it useful?
Function Get-Acronym() {
[CmdletBinding()]
Param (
[string]$Phrase
)
( $Phrase -replace "[-_]"," " -split " " | Foreach-Object {$_[0].ToString().ToUpper()} ) -join ""
}
Get-Date cmdlet.
Function Get-Acronym() {
[CmdletBinding()]
Param (
[string]$Phrase
)
(-join ([regex]::Matches($Phrase, "\p{L}+'?\p{L}*") | ForEach-Object { $_.Value[0] })).ToUpper()
}
Approach using regular expression to deal with string
foreach ($char in $Phrase.ToCharArray()) {
if ($newWord -and $char -match "[a-z]") {
$acronym += $char
$newWord = $false
}
if (-not $newWord -and $char -match "[_\- ]") {
$newWord = $true
}
Approach using foreach loop and boolean flag