获取字符串数组的子字符串的最佳方法是什么?
我有这样的事情:
string[] split = ListOfUsers.Split(new Char[] { ';', ',', ' ' });
“split”中的每个字符串开头都有一个“@”符号,我想去掉它。在不进入 for 循环的情况下获取该数组的子字符串的理想方法是什么?我可以使用 Linq 来代替吗?
感谢您的浏览:)
I have something like this:
string[] split = ListOfUsers.Split(new Char[] { ';', ',', ' ' });
Each string in "split" has an "@" sign at the beginning, and I want to get rid of it. What would be the ideal way to get the substring of this array, without getting into a for loop. Can I use Linq instead?
Thanks for looking :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
String.Trim
超载正是这样做的:从字符串的开头和结尾删除指定的字符:如果您的字符串在开头或结尾包含多个
@
,或以@
结尾,则可以这样做超出你的预期。在这种情况下,您也可以简单地使用String.Substring
:There is an overload of
String.Trim
that does exactly this: removes the specified characters from the beginning and end of a string:If your strings contain multiple
@
s in the beginning or end with a@
this will do more than you intended. In that case, you can also simply useString.Substring
:除了修剪之外,正如 Jon 建议的那样,您还可以使用正则表达式:
Beside trimming, as Jon suggested you can use regular expressions:
可能只是添加
,这样每个子字符串都会有您需要的字符串。
May be just add
so every sub-string will have the string you need.