Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a trailing comma from a string

I have a string producing results in PowerShell being separated by a comma, which is fine, but I don't want the last comma.

I understand a Trim function is required, but I can't seem to get it to work. Maybe i've been trying it in the wrong place?

I need it to return three "Site Codes, each separated with a comma, and no comma at the end. I need to know where to add the trim bit really.

$UKPRN = "10007405"
$str_sites = "A4EYORK,AVLEEDS,BANBURY";

$sites = "";


foreach ($site in $str_sites.Split(","))
{
$sites = $sites+"'"+$site+"',"

}
Write-Output $sites;

1 Answers

You could just use TrimEnd()

$str = "string with , at the end,"
$str.TrimEnd(",")
like image 200
TobyU Avatar answered Sep 13 '25 15:09

TobyU