skearney
(Shannon Kearney)
June 1, 2021, 6:20pm
1
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);
}
jkane
(John Kane)
June 1, 2021, 6:26pm
2
is it supposed to be
tbSelectedWeeklyPrint.Text.TrimEnd(";");
timshuwy
(Tim Shoemaker)
June 1, 2021, 6:29pm
3
try this:
tbSelectedWeeklyPrint.Text = tbSelectedWeeklyPrint.Text.TrimEnd(';');
or better yet, you can do this:
equipIDs = strBuilder.ToString().TrimEnd(';');
Doug.C
(Doug Crabtree)
June 1, 2021, 6:36pm
4
Yeah, what @timshuwy said (I like the second one). The TrimEnd()
function returns a string, it doesn’t modify the original…
1 Like
skearney
(Shannon Kearney)
June 1, 2021, 8:12pm
5
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(',');
}
timshuwy
(Tim Shoemaker)
June 1, 2021, 10:11pm
6
@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(',');
}