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

栏目: C# · 发布时间: 5年前

内容简介:http://stackoverflow.com/questions/15564944/remove-the-last-three-characters-from-a-string

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

string myString = "abcdxxx";

请注意,该字符串是动态数据.

read last 3 characters from string [Initially asked question]

您可以使用 string.Substring 并给出起始索引,它将从给定的索引到结尾处获得子串.

myString.Substring(str.Length-3)
Retrieves a substring from this instance. The substring starts at a  specified character position. 07001

编辑,更新的帖子

Remove last 3 characters from string [Updated question]

要从字符串中删除最后三个字符,您可以使用 string.Substring(Int32, Int32) ,并将其起始索引0和结束索引缩小到字符串长度的三分之一.它会在最后三个字符之前得到子字符串.

myString = myString.Substring(0, str.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) 方法通过将起始索引作为长度-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

http://stackoverflow.com/questions/15564944/remove-the-last-three-characters-from-a-string


以上所述就是小编给大家介绍的《c# – 从字符串中删除最后三个字符》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

欲罢不能

欲罢不能

[美] 亚当·奥尔特 / 闾佳 / 机械工业出版社 / 2018-4-1 / 59.00元

全面揭秘和解决“行为上瘾”的奠基之作 美国亚马逊分类图书畅销榜第一名 行为上瘾是什么?诱人上瘾的体验是如何设计出来的? 如何远离行为上瘾?如何用行为上瘾做些好事? ◆ 内容简介 ◆ 欢迎来到“行为上瘾”的时代! 我们中近半数人至少有一种“行为上瘾”:无时无刻盯着手机,不断刷朋友圈,通宵追看电视剧集,没日没夜打游戏,频繁查看邮件,用太多时间工作…… 而那些生......一起来看看 《欲罢不能》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具