删除字符串的最后一个字符

发布于 2024-12-11 20:32:26 字数 425 浏览 0 评论 0原文

我正在检索链接到数据库的列表中的大量信息,并且我想为连接到该网站的人创建一系列组。

我用它来测试,但这不是动态的,所以它真的很糟糕:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

∞梦里开花 2024-12-18 20:32:26
strgroupids = strgroupids.Remove(strgroupids.Length - 1);

MSDN:

String.Remove(Int32):

删除该字符串中从指定位置开始的所有字符
位置并继续到最后一个位置

strgroupids = strgroupids.Remove(strgroupids.Length - 1);

MSDN:

String.Remove(Int32):

Deletes all the characters from this string beginning at a specified
position and continuing through the last position

夏至、离别 2024-12-18 20:32:26

这样做怎么样?

strgroupids = string.Join( ",", groupIds );

更干净。

它将附加 groupIds 内的所有元素,并在每个元素之间添加 ',',但不会在末尾放置 ','

What about doing it this way

strgroupids = string.Join( ",", groupIds );

A lot cleaner.

It will append all elements inside groupIds with a ',' between each, but it will not put a ',' at the end.

倚栏听风 2024-12-18 20:32:26

In C# 8 ranges and indices were introduced, giving us a new more succinct solution:

strgroupids = strgroupids[..^1];
雪落纷纷 2024-12-18 20:32:26

C# 中的字符串是不可变的。当您在代码中执行 strgroupids.TrimEnd(',');strgroupids.TrimEnd(new char[] { ',' }); 时,strgroupids 字符串未修改

您需要执行类似 strgroupids = strgroupids.TrimEnd(','); 的操作。

引用此处

字符串是不可变的——字符串对象的内容不能改变
尽管语法使得它在创建对象后发生了变化
看起来好像你能做到这一点。例如,当您编写这段代码时,
编译器实际上创建了一个新的字符串对象来保存新的
字符序列,并且该新对象被分配给 b。这
然后字符串“h”就有资格进行垃圾回收。

Strings in c# are immutable. When in your code you do strgroupids.TrimEnd(','); or strgroupids.TrimEnd(new char[] { ',' }); the strgroupids string is not modified.

You need to do something like strgroupids = strgroupids.TrimEnd(','); instead.

To quote from here:

Strings are immutable--the contents of a string object cannot be
changed after the object is created, although the syntax makes it
appear as if you can do this. For example, when you write this code,
the compiler actually creates a new string object to hold the new
sequence of characters, and that new object is assigned to b. The
string "h" is then eligible for garbage collection.

心是晴朗的。 2024-12-18 20:32:26

添加扩展方法。

public static string RemoveLast(this string text, string character)
{
    if(text.Length < 1) return text;
    return text.Remove(text.ToString().LastIndexOf(character), character.Length);
}

然后使用:

yourString.RemoveLast(",");

Add an extension method.

public static string RemoveLast(this string text, string character)
{
    if(text.Length < 1) return text;
    return text.Remove(text.ToString().LastIndexOf(character), character.Length);
}

then use:

yourString.RemoveLast(",");
爱,才寂寞 2024-12-18 20:32:26

删除所有尾随逗号:

while (strgroupids.EndsWith(","))
    strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);

不过这是向后的,您编写了首先添加逗号的代码。您应该使用 string.Join(",",g) 代替,假设 gstring[]。也给它一个比 g 更好的名字!

Removes any trailing commas:

while (strgroupids.EndsWith(","))
    strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);

This is backwards though, you wrote the code that adds the comma in the first place. You should use string.Join(",",g) instead, assuming g is a string[]. Give it a better name than g too !

只有一腔孤勇 2024-12-18 20:32:26

sll 解决方案的补充:最好修剪字符串,以防末尾有一些空格。

strgroupids = strgroupids.Remove(strgroupids.Trim().Length - 1);

Additional to sll's solution: It's better to trim the string in case there are some blank(s) at the end.

strgroupids = strgroupids.Remove(strgroupids.Trim().Length - 1);
零崎曲识 2024-12-18 20:32:26

作为为每个项目添加逗号的替代方法,您可以使用 String.Join:

var strgroupids = String.Join(",",  groupIds);

这将在数组中的每个元素之间添加分隔符(在本例中为“,”)。

As an alternate to adding a comma for each item you could just using String.Join:

var strgroupids = String.Join(",",  groupIds);

This will add the seperator ("," in this instance) between each element in the array.

瞎闹 2024-12-18 20:32:26
string strgroupids = string.Empty;

groupIds.ForEach(g =>
{
    strgroupids = strgroupids + g.ToString() + ",";
});

strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);

请注意,此处使用 ForEach 通常被认为是“错误的”(例如阅读 http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx)

使用一些 LINQ:

string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => p + q + ',');
strgroupids = strgroupids.Substring(0, str1.Length - 1);

没有结尾子字符串:

string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => (p != string.Empty ? p + "," + q : q.ToString()));
string strgroupids = string.Empty;

groupIds.ForEach(g =>
{
    strgroupids = strgroupids + g.ToString() + ",";
});

strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);

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:

string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => p + q + ',');
strgroupids = strgroupids.Substring(0, str1.Length - 1);

Without end-substringing:

string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => (p != string.Empty ? p + "," + q : q.ToString()));
泪之魂 2024-12-18 20:32:26

string.Join 更好,但如果您真的想要一个 LINQ ForEach

var strgroupids = string.Empty;

groupIds.ForEach(g =>
{
    if(strgroupids != string.Empty){
        strgroupids += ",";
    }

    strgroupids += g;
});

一些注意事项:

  • string.Joinforeach 都比这个好,但速度慢得多,
  • 不需要删除最后一个 ,,因为它从未附加
  • 增量运算符 (+=)可以方便地附加到字符串
  • .ToString() 是不必要的,因为它在连接非字符串时自动调用
  • 。处理大字符串时,应考虑 StringBuilder 而不是连接字符串

string.Join is better, but if you really want a LINQ ForEach:

var strgroupids = string.Empty;

groupIds.ForEach(g =>
{
    if(strgroupids != string.Empty){
        strgroupids += ",";
    }

    strgroupids += g;
});

Some notes:

  • string.Join and foreach are both better than this, vastly slower, approach
  • No need to remove the last , since it's never appended
  • The increment operator (+=) is handy for appending to strings
  • .ToString() is unnecessary as it is called automatically when concatenating non-strings
  • When handling large strings, StringBuilder should be considered instead of concatenating strings
触ぅ动初心 2024-12-18 20:32:26

没有“快速而肮脏”的方法可以做到这一点。我通常这样做:

mystring= string.Concat(mystring.Take(mystring.Length-1));

There is no "quick-and-dirty" way of doing this. I usually do:

mystring= string.Concat(mystring.Take(mystring.Length-1));
你在看孤独的风景 2024-12-18 20:32:26

该代码删除字符串

string myString = "Hello;";    
myString = myString.Remove(myString.Length-1);

输出

中的最后一个字符

你好

That code delete the last character in a string

string myString = "Hello;";    
myString = myString.Remove(myString.Length-1);

Output

Hello

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文