[英]A String manipulation Question for .NET
我怎么能从下面删除仅第一个逗号+空格,如果它在那里什么也没做。
string comments = ", 38, ";
if( comments.StartsWith(", ") && comments.Length > 2 ) {
comments = comments.Substring(2);
}
The best way would be to use the String.TrimStart(...)
method. 最好的方法是使用
String.TrimStart(...)
方法。
string comments = ", 38, ";
string commentsOK = "38, ";
string trimmedComments = comments.TrimStart(',', ' ');
string trimmedCommentsOK = commentsOK.TrimStart(',', ' ');
After this both trimmedComments
and trimmedCommentsOK
would have the value "38, "
. 此后,
trimmedComments
和trimmedCommentsOK
都将具有值"38, "
。
String.TrimStart
method reference: http://msdn.microsoft.com/en-us/library/system.string.trimstart.aspx String.TrimStart
方法参考: http : //msdn.microsoft.com/zh-cn/library/system.string.trimstart.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.