简体   繁体   English

如何使用 c# 替换字符串的最后一个字符?

[英]How to replace last character of the string using c#?

string str = "Student_123_";

I need to replace the last character "_" with ",".我需要用“,”替换最后一个字符“_”。 I did it like this.我是这样做的。

str.Remove(str.Length -1, 1);
str = str + ",";

However, is it possible to achieve it more efficiently.但是,是否有可能更有效地实现它。 may be one line of code.??可能是一行代码。?? BTW, last character can be any character.顺便说一句,最后一个字符可以是任何字符。 So Replace wont work here.所以替换在这里不起作用。

No.不。

In C# strings are immutable and thus you can not change the string "in-place".在 C# 中,字符串是不可变的,因此您不能“就地”更改字符串。 You must first remove a part of the string and then create a new string.您必须先删除部分字符串,然后再创建一个新字符串。 In fact, this is also means your original code is wrong, since str.Remove(str.Length -1, 1);实际上,这也意味着您的原始代码是错误的,因为str.Remove(str.Length -1, 1); doesn't change str at all, it returns a new string: This should do:根本不改变 str ,它返回一个新字符串:这应该这样做:

str = str.Remove(str.Length -1, 1) + ",";

C# .NET makes it almost too easy. C# .NET 让它几乎太容易了。

str = str.TrimEnd('_')

That's a limitation of working with string .这是使用string的限制。 You can use StringBuilder if you need to do a lot of changes like this.如果你需要做很多这样的改变,你可以使用StringBuilder But it's not worth it for the simple task you need.但是对于您需要的简单任务,这不值得。

str = str.Substring(0, str.Length - 1) + ",";

Elegant but not very efficient.优雅但效率不高。 Replaces any character at the end of str with a comma.用逗号替换 str 末尾的任何字符。

str = Regex.Replace(str, ".$", ",");

Use the StringBuilder class使用 StringBuilder class

StringBuilder mbuilder = new StringBuilder("Student_123_");
mbuilder[mbuilder.Length-1] = ',';
Console.WriteLine(mbuilder.ToString());

str = str.Substring(0, str.Length-1) + ",";

Well, what you have won't work because str.Remove(...) doesn't manipulate str , it returns a new string with the removal operation completed on it.好吧,您将无法使用,因为str.Remove(...)不操作str ,它返回一个新字符串,并在其上完成了删除操作。

So - you need:所以 - 你需要:

str = str.Remove(str.Length-1,1);
str = str + ",";

In terms of efficiency, there are several other choices you could make (substring, trim...) but ultimately you're going to get the same time/space complexity.在效率方面,您可以做出其他几种选择(子字符串、修剪...),但最终您将获得相同的时间/空间复杂度。

EDIT:编辑:

Also, don't try to squash everything into one line, the programmers who come after you will appreciate the greater readability.另外,不要试图将所有内容压缩到一行中,追随你的程序员会欣赏更高的可读性。 (Although in this case a single line is just as easy to read.) One line.= more efficient. (尽管在这种情况下,一行也很容易阅读。)一行。= 更有效。

With one line of code you could write:使用一行代码,您可以编写:

str = str.Remove(str.Length - 1, 1) + ",";

str.Remove doesn't modify str , it returns a new string. str.Remove不修改str ,它返回一个新字符串。 Your first line should read str = str.Remove...您的第一行应为str = str.Remove...

One line?一条线? OK: str = str.Remove(str.Length - 1) + ",";好的: str = str.Remove(str.Length - 1) + ",";

I think that's as efficient as you're going to get.我认为这与您将获得的一样有效。 Technically, you are creating two new strings here, not one (The result of the Remove, and the result of the Concatenation).从技术上讲,您在这里创建了两个新字符串,而不是一个(Remove 的结果和 Concatenation 的结果)。 However, everything I can think of to not create two strings, ends up creating more than 1 other object to do so.但是,我能想到的不创建两个字符串的一切,最终都会创建超过 1 个其他 object 来这样做。 You could use a StringBuilder, but that's heavier weight than an extra string, or perhaps a char[] , but it's still an extra object, no better than what I have listed above.您可以使用 StringBuilder,但这比额外的字符串或char[]更重,但它仍然是额外的 object,并不比我上面列出的要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM