gpt4 book ai didi

c# - 从字符串中删除最后三个字符

转载 作者:IT王子 更新时间:2023-10-29 03:36:24 27 4
gpt4 key购买 nike

我想从字符串中删除最后三个字符:

string myString = "abcdxxx"; 

注意字符串是动态数据。

最佳答案

read last 3 characters from string [Initially asked question]

您可以使用 string.Substring并给它起始索引,它将获得从给定索引开始到结束的子字符串。

myString.Substring(myString.Length-3)

Retrieves a substring from this instance. The substring starts at a specified character position. MSDN

编辑,更新帖子

Remove last 3 characters from string [Updated question]

要从字符串中删除最后三个字符,您可以使用 string.Substring(Int32, Int32)并为其指定起始索引 0 和结束索引 比字符串长度小三。它将获取最后三个字符之前的子字符串。

myString = myString.Substring(0, myString.Length-3);

String.Substring Method (Int32, Int32)

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

您还可以使用 String.Remove(Int32)方法通过将起始索引作为 length - 3 来删除最后三个字符,它将从该点删除到字符串的末尾。

myString = myString.Remove(myString.Length-3)

String.Remove Method (Int32)

Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted

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

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