Question

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.

Was it helpful?

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

OTHER TIPS

string s = ",liger, unicorn, snipe";
s.TrimStart(',');

.net strings can do Trim() and TrimStart(). Because it takes params, you can write:

",liger, unicorn, snipe".TrimStart(',')

and if you have more than one character to trim, you can write:

",liger, unicorn, snipe".TrimStart(",; ".ToCharArray())

here is an easy way to not produce the leading comma to begin with:

string[] animals = { "liger", "unicorn", "snipe" };
string joined = string.Join(", ", animals);

string.TrimStart(',') will remove the comma, however you will have trouble with a split operation due to the space after the comma. Best to join just on the single comma or use

Split(", ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);

",liger, unicorn, snipe".Trim(',') -> "liger, unicor, snipe"

Try string.Trim(',') and see if that does what you want.

Note, the original string is left untouched, Trim will return you a new string:

string s1 = ",abc,d";
string s2 = s1.TrimStart(",".ToCharArray());
Console.WriteLine("s1 = {0}", s1);
Console.WriteLine("s2 = {0}", s2);

prints:

s1 = ,abc,d
s2 = abc,d
string s = ",liger, unicorn, snipe";
s = s.TrimStart(',');

It's important to assign the result of TrimStart to a variable. As it says on the TrimStart page, "This method does not modify the value of the current instance. Instead, it returns a new string...".

In .NET, strings don't change.

you can use this

,liger, unicorn, snipe".TrimStart(',');

if (s.StartsWith(",")) {
    s = s.Substring(1, s.Length - 1);
}
string t = ",liger, unicorn, snipe".TrimStart(new char[] {','});

The same way as everywhere else: string.trim

    string s = ",liger, tiger";

    if (s.Substring(0, 1) == ",")
        s = s.Substring(1);

Did you mean trim all instances of "," in that string?

In which case, you can do:

s = s.Replace(",", "");

Just use Substring to ignore the first character (or assign it to another string);

 string o = ",liger, unicorn, snipe";
 string s = o.Substring(1);

See: http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx

        string animals = ",liger, unicorn, snipe";

        //trimmed will contain "liger, unicorn, snipe"
        string trimmed = word.Trim(',');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top