Trim End not removing last character

Has anyone had issues with TrimEnd not working. I have the following code and no matter what I try it isn’t trimming the last semicolon.

private void PMEquipString()
	{
	string strBuilder = "";
		equipIDs = string.Empty;
		foreach (UltraGridRow row in gridPMSWeekly.Selected.Rows)
		{
			strBuilder += row.Cells["Equip_EquipID"].Text.ToString() + ";";
		
	}
		equipIDs = strBuilder.ToString();
		tbSelectedWeeklyPrint.Text = equipIDs.ToString();
		tbSelectedWeeklyPrint.Text.TrimEnd(';');
		MessageBox.Show(tbSelectedWeeklyPrint.Text);
}

image

is it supposed to be

tbSelectedWeeklyPrint.Text.TrimEnd(";");

try this:

tbSelectedWeeklyPrint.Text = tbSelectedWeeklyPrint.Text.TrimEnd(';');

or better yet, you can do this:

equipIDs = strBuilder.ToString().TrimEnd(';');

Yeah, what @timshuwy said (I like the second one). The TrimEnd() function returns a string, it doesn’t modify the original…

1 Like

This worked!

	private void PMEquipString()
	{
	string strBuilder = "";
		equipIDs = string.Empty;
		foreach (UltraGridRow row in gridPMSWeekly.Selected.Rows)
		{
		strBuilder += row.Cells["Equip_EquipID"].Text.ToString() + ",";
	}
		equipIDs = strBuilder.ToString().TrimEnd(',');
}

@skearney I just noticed that your code appends strings, but you are not using a true StringBuilder (see C# StringBuilder Examples - Dot Net Perls). This is not as efficient. You should consider changing the code to something like this (only 2 lines of code changed… :

private void PMEquipString() {
    StringBuilder strBuilder = new StringBuilder();    //<------new change
    equipIDs = string.Empty;
    foreach (UltraGridRow row in gridPMSWeekly.Selected.Rows) {
        strBuilder.Append(row.Cells["Equip_EquipID"].Text.ToString() + ",");  //<------new change
    }
    equipIDs = strBuilder.ToString().TrimEnd(',');
}