从字符串中删除最后三个字符 [英] Remove the last three characters from a string

查看:1619
本文介绍了从字符串中删除最后三个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 字符串的myString =abcdxxx; 

请注意该字符串是动态的数据。


解决方案

阅读最后从字符串3字符[最初问的问题]




您可以使用 string.Substring 并给它的起始索引,它会得到从子给定索引到年底开始。

  myString.Substring(str.Length-3)




检索从这个实例中的子字符串。该子开始在
指定字符位置。 MSDN




修改,更新的职位




删除最后3个字符从string [更新问题]




要删除字符串可以使用的string.Substring(Int32,Int32)将并给它的起始索引 0 和结束索引的三少那么字符串长度的。它会得到最后三个字符前的子字符串。

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

String.Substring方法(Int32,Int32)将




检索子从这个实例。该子开始在
指定字符位置,并有指定的长度。




您还可以使用的String.Remove(Int32)方法通过传递开始索引,以消除最后三个字符的长度 - 3 的,它会从这个角度删除结束串

 的myString = myString的。删除(myString.Length-3)

String.Remove方法(Int32)




返回这在目前的
实例中的所有角色,开始在指定的位置,并通过
继续最后一个位置,已被删除一个新字符串



I want to remove last three characters from a string:

string myString = "abcdxxx"; 

Note that the string is dynamic data.

解决方案

read last 3 characters from string [Initially asked question]

You can use string.Substring and give it the starting index and it will get the substring starting from given index till end.

myString.Substring(str.Length-3)

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

Edit, for updated post

Remove last 3 characters from string [Updated question]

To remove last three characters from string you can use string.Substring(Int32, Int32) and give it the starting index 0 and end index three less then the string length. It will get the substring before last three characters.

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.

You can also using String.Remove(Int32) method to remove the last three characters by passing start index as length - 3, it will remove from this point to end of string.

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

这篇关于从字符串中删除最后三个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆