-3

I have this string:

string ID = 6FA186D2-1246-4691-8560-7BDAC7D10699

And I tried to get rid of the "-", so I went with:

    private string FormatProductID()
    {
        return = selectedPackage.id.Trim('-');
    }

But this returns only the original string, and doenst alter it at all. I tried using "–" instead of "-" but the result is the same.

Am I using this function wrong?

Thanks!

DiplomacyNotWar
  • 31,605
  • 6
  • 57
  • 75
  • 2
    Trim is for removing whitespaces inside a string, However if you want to remove "-" you could use string.replace("-", string.empty). Ref: https://stackoverflow.com/questions/7411438/remove-characters-from-c-sharp-string – yazan Oct 08 '20 at 12:11
  • 1
    Does this answer your question? [Remove characters from C# string](https://stackoverflow.com/questions/7411438/remove-characters-from-c-sharp-string) – nalka Oct 08 '20 at 12:13
  • That's a common GUID format . It would make more sense to parse it to a Guid with `Guid.Parse` than remove the dashes. It would use *less* memory too - all string operations create new strings. A Guid is 16 bytes – Panagiotis Kanavos Oct 08 '20 at 12:14
  • @yazan: "inside" is a very confusing word to use when describing `Trim`. `Trim` specifically focuses on the **edges** (start/end) of a string. – Flater Oct 08 '20 at 12:21

3 Answers3

3

The Trim method only removes the characters from the beginning and end.

You are looking for the Replace method.

private string FormatProductID()
{
    return selectedPackage.id.Replace("-","");
}
GvS
  • 51,423
  • 16
  • 99
  • 138
1

You should use Replace

private string FormatProductID()
{
    return = selectedPackage.id.Replace("-","");
}
Shinva
  • 1,546
  • 16
  • 24
0

You picked the wrong method, try

private string FormatProductID()
{
    return selectedPackage.id.Replace("-", string.Empty);
}
Matthias
  • 104
  • 1
  • 7