RosettaCodeData/Task/Comma-quibbling/PowerShell/comma-quibbling-1.psh
2016-12-05 22:15:40 +01:00

41 lines
898 B
Text

function Out-Quibble
{
[OutputType([string])]
Param
(
# Zero or more strings.
[Parameter(Mandatory=$false, Position=0)]
[AllowEmptyString()]
[string[]]
$Text = ""
)
# If not null or empty...
if ($Text)
{
# Remove empty strings from the array.
$text = "$Text".Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
}
else
{
return "{}"
}
# Build a format string.
$outStr = ""
for ($i = 0; $i -lt $text.Count; $i++)
{
$outStr += "{$i}, "
}
$outStr = $outStr.TrimEnd(", ")
# If more than one word, insert " and" at last comma position.
if ($text.Count -gt 1)
{
$cIndex = $outStr.LastIndexOf(",")
$outStr = $outStr.Remove($cIndex,1).Insert($cIndex," and")
}
# Output the formatted string.
"{" + $outStr -f $text + "}"
}