Search code examples
c#.netstringtrim

How can I Trim the leading comma in my string


I have a string that is like below.

,liger, unicorn, snipe

in other languages I'm familiar with I can just do a string.trim(",") but how can I do that in c#?

Thanks.


There's been a lot of back and forth about the StartTrim function. As several have pointed out, the StartTrim doesn't affect the primary variable. However, given the construction of the data vs the question, I'm torn as to which answer to accept. True the question only wants the first character trimmed off not the last (if anny), however, there would never be a "," at the end of the data. So, with that said, I'm going to accept the first answer that that said to use StartTrim assigned to a new variable.


Solution

  • string sample = ",liger, unicorn, snipe";
    sample = sample.TrimStart(','); // to remove just the first comma
    

    Or perhaps:

    sample = sample.Trim().TrimStart(','); // to remove any whitespace and then the first comma