删除字符串的最后一个字符
我正在检索链接到数据库的列表中的大量信息,并且我想为连接到该网站的人创建一系列组。
我用它来测试,但这不是动态的,所以它真的很糟糕:
string strgroupids = "6";
我现在想使用它。但返回的字符串类似于 1,2,3,4,5,
groupIds.ForEach((g) =>
{
strgroupids = strgroupids + g.ToString() + ",";
strgroupids.TrimEnd(',');
});
strgroupids.TrimEnd(new char[] { ',' });
我想删除 5
之后的 ,
但它肯定是不工作。
I am retrieving a lot of information in a list, linked to a database and I want to create a string of groups, for someone who is connected to the website.
I use this to test but this is not dynamic, so it is really bad:
string strgroupids = "6";
I want to use this now. But the string returned is something like 1,2,3,4,5,
groupIds.ForEach((g) =>
{
strgroupids = strgroupids + g.ToString() + ",";
strgroupids.TrimEnd(',');
});
strgroupids.TrimEnd(new char[] { ',' });
I want to delete the ,
after the 5
but it's definitely not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
MSDN:
MSDN:
这样做怎么样?
更更干净。
它将附加
groupIds
内的所有元素,并在每个元素之间添加','
,但不会在末尾放置','
。What about doing it this way
A lot cleaner.
It will append all elements inside
groupIds
with a','
between each, but it will not put a','
at the end.在 C# 8 中 范围和索引< /a> 的引入,给了我们一个新的更简洁的解决方案:
In C# 8 ranges and indices were introduced, giving us a new more succinct solution:
C# 中的字符串是不可变的。当您在代码中执行
strgroupids.TrimEnd(',');
或strgroupids.TrimEnd(new char[] { ',' });
时,strgroupids
字符串未修改。您需要执行类似
strgroupids = strgroupids.TrimEnd(',');
的操作。引用此处:
Strings in c# are immutable. When in your code you do
strgroupids.TrimEnd(',');
orstrgroupids.TrimEnd(new char[] { ',' });
thestrgroupids
string is not modified.You need to do something like
strgroupids = strgroupids.TrimEnd(',');
instead.To quote from here:
添加扩展方法。
然后使用:
Add an extension method.
then use:
删除所有尾随逗号:
不过这是向后的,您编写了首先添加逗号的代码。您应该使用
string.Join(",",g)
代替,假设g
是string[]
。也给它一个比g
更好的名字!Removes any trailing commas:
This is backwards though, you wrote the code that adds the comma in the first place. You should use
string.Join(",",g)
instead, assumingg
is astring[]
. Give it a better name thang
too !sll 解决方案的补充:最好修剪字符串,以防末尾有一些空格。
Additional to sll's solution: It's better to trim the string in case there are some blank(s) at the end.
作为为每个项目添加逗号的替代方法,您可以使用 String.Join:
这将在数组中的每个元素之间添加分隔符(在本例中为“,”)。
As an alternate to adding a comma for each item you could just using String.Join:
This will add the seperator ("," in this instance) between each element in the array.
请注意,此处使用
ForEach
通常被认为是“错误的”(例如阅读 http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx)使用一些 LINQ:
没有结尾子字符串:
Note that the use of
ForEach
here is normally considered "wrong" (read for example http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx)Using some LINQ:
Without end-substringing:
string.Join
更好,但如果您真的想要一个 LINQForEach
:一些注意事项:
string.Join
和foreach
都比这个好,但速度慢得多,,
,因为它从未附加+=
)可以方便地附加到字符串.ToString()
是不必要的,因为它在连接非字符串时自动调用StringBuilder
而不是连接字符串string.Join
is better, but if you really want a LINQForEach
:Some notes:
string.Join
andforeach
are both better than this, vastly slower, approach,
since it's never appended+=
) is handy for appending to strings.ToString()
is unnecessary as it is called automatically when concatenating non-stringsStringBuilder
should be considered instead of concatenating strings没有“快速而肮脏”的方法可以做到这一点。我通常这样做:
There is no "quick-and-dirty" way of doing this. I usually do:
该代码删除字符串
输出
That code delete the last character in a string
Output