tags:

views:

2587

answers:

17

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.

A: 
if (s.StartsWith(",")) {
    s = s.Substring(1, s.Length - 1);
}
Bob King
+16  A: 
   string s = ",liger, unicorn, snipe";
   s.TrimStart(',');
DevelopingChris
Does that method _modifies_ the string? Shouldn't it be re-assigned back to s ala s = s.TrimStrat('/'); ?
chakrit
not sure, but its non-sequitor to the question.
DevelopingChris
A: 
string t = ",liger, unicorn, snipe".TrimStart(new char[] {','});
Kieran Benton
A: 

The same way as everywhere else: string.trim

Nouveau
A: 

string s = ",liger, tiger";

    if (s.Substring(0, 1) == ",")
        s = s.Substring(1);
Matt Dawdy
just fyi, you can do s.StartsWith(, and not have to actually do all the substring just to get the first character.
DevelopingChris
Thanks ChanChan. I often fall back on a small subset of .Net that I "expect" to be there, based on my previous experience with other languages.
Matt Dawdy
A: 

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

In which case, you can do:

s = s.Replace(",", "");
chakrit
He just wants to remove the first comma.
David Smart
+1  A: 

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

Akselsson
The question asks to remove the 'leading comma', but Trim() "Removes all leading and trailing occurrences of a set of specified characters from the current String object." http://msdn.microsoft.com/en-us/library/system.string.trim.aspx
Andrew
+1  A: 

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

Magus
The question asks to remove the 'leading comma', but Trim() "Removes all leading and trailing occurrences of a set of specified characters from the current String object." http://msdn.microsoft.com/en-us/library/system.string.trim.aspx
Andrew
A: 

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

 string o = ",liger, unicorn, snipe";
 string s = o.Substring(1);
clintp
+4  A: 

.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())
Jay Bazuzi
wow.. that string to char array is one nice trick I've never saw before.
chakrit
A: 

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(',');
bentford
+1  A: 
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
RickL
+1  A: 

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
hamishmcn
+2  A: 

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

string[] animals = { "liger", "unicorn", "snipe" };
string joined = string.Join(", ", animals);
justin.m.chase
+1  A: 

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.

Wonko
+1  A: 

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

abjbhat
+1  A: 

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);

benPearce