如何修剪字符串中的前导逗号

Ken*_*eng 10 .net c# string trim

我有一个字符串,如下所示.

,liger, unicorn, snipe
Run Code Online (Sandbox Code Playgroud)

在我熟悉的其他语言中,我可以做一个string.trim(","),但我怎样才能在c#中做到这一点?

谢谢.


关于StartTrim功能有很多来回.正如几位人士指出的那样,StartTrim不会影响主变量.但是,考虑到数据与问题的构建,我对于接受哪个答案感到困惑.确实,问题只是想要修剪第一个字符而不是最后一个字符(如果是anny),但是,数据末尾永远不会有",".所以,话虽如此,我将接受第一个回答,即说使用StartTrim分配给一个新变量.

Dev*_*ris 21

string s = ",liger, unicorn, snipe";
s.TrimStart(',');
Run Code Online (Sandbox Code Playgroud)


Ric*_*ckL 12

string sample = ",liger, unicorn, snipe";
sample = sample.TrimStart(','); // to remove just the first comma
Run Code Online (Sandbox Code Playgroud)

也许:

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