C# 从字符串中删除最后三个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15564944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove the last three characters from a string
提问by user1765862
I want to remove last three characters from a string:
我想从字符串中删除最后三个字符:
string myString = "abcdxxx";
Note that the string is dynamic data.
请注意,该字符串是动态数据。
采纳答案by Adil
read last 3 characters from string [Initially asked question]
从字符串中读取最后 3 个字符 [最初提出的问题]
You can use string.Substringand give it the starting index and it will get the substring starting from given index till end.
您可以使用string.Substring并为其指定起始索引,它将获取从给定索引开始到结束的子字符串。
myString.Substring(myString.Length-3)
Retrieves a substring from this instance. The substring starts at a specified character position. MSDN
从此实例中检索子字符串。子字符串从指定的字符位置开始。MSDN
Edit, for updated post
编辑,更新后的帖子
Remove last 3 characters from string [Updated question]
从字符串中删除最后 3 个字符 [更新的问题]
To remove the last three characters from the string you can use string.Substring(Int32,?Int32)and give it the starting index 0
and end index three less than the string length. It will get the substring before last three characters.
要从字符串中删除最后三个字符,您可以使用string.Substring(Int32,?Int32)并为其指定比字符串 length 小 3的起始索引0
和结束索引。它将获取最后三个字符之前的子字符串。
myString = myString.Substring(0, myString.Length-3);
String.Substring Method (Int32,?Int32)
String.Substring 方法 (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.
您还可以使用String.Remove(Int32)方法通过将起始索引作为length - 3传递来删除最后三个字符,它将从该点删除到字符串末尾。
myString = myString.Remove(myString.Length-3)
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
返回一个新字符串,其中当前实例中从指定位置开始到最后一个位置的所有字符都已被删除
回答by Alex
myString = myString.Remove(myString.Length - 3, 3);
回答by Adeel
myString.Remove(myString.Length-3);
回答by scofield
str= str.Remove(str.Length - 3);
str= str.Remove(str.Length - 3);
回答by lc.
You can use String.Remove
to delete from a specified position to the end of the string.
您可以使用String.Remove
从指定位置删除到字符串末尾。
myString = myString.Remove(myString.Length - 3);
回答by Freelancer
myString.Substring(myString.Length - 3, 3)
Here are examples on substring.>>
以下是子字符串的示例。>>
http://www.dotnetperls.com/substring
http://www.dotnetperls.com/substring
Refer those.
参考那些。
回答by Frank59
string myString = "abcdxxx";
if (myString.Length<3)
return;
string newString=myString.Remove(myString.Length - 3, 3);
回答by Jaidi Wael Leila
string test = "abcdxxx";
test = test.Remove(test.Length - 3);
//output : abcd
回答by Byron Katz
I read through all these, but wanted something a bit more elegant. Just to remove a certain number of characters from the end of a string:
我通读了所有这些,但想要一些更优雅的东西。只是从字符串的末尾删除一定数量的字符:
string.Concat("hello".Reverse().Skip(3).Reverse());
output:
输出:
"he"
回答by subramanya46
Remove the last characters from a string
从字符串中删除最后一个字符
TXTB_DateofReiumbursement.Text = (gvFinance.SelectedRow.FindControl("lblDate_of_Reimbursement") as Label).Text.Remove(10)
.Text.Remove(10)
// used to remove text starting from index 10 to end
.Text.Remove(10)
// 用于删除从索引 10 开始到结尾的文本