gpt4 book ai didi

c# - 从字符串中删除单个空格

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:39 25 4
gpt4 key购买 nike

"How do I do this?              "

假设我有这个字符串。如何从末尾只删除一个空格?下面显示的代码给我一个错误,说计数超出范围。

string s = "How do I do this?              ";
s = s.Remove(s.Length, 1);

最佳答案

您只需要改用它:

string s = "How do I do this?              ";
s = s.Remove(s.Length-1, 1);

如前所述here :

Remove(Int32) Returns a new string in which all the characters in the currentinstance, beginning at a specified position and continuing through thelast position, have been deleted.

在数组中,位置范围从 0 到 Length-1,因此会出现编译错误。

关于c# - 从字符串中删除单个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18774536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com