从字符串中删除最后三个字符

时间:2023-02-07 17:07:24

I want to remove last three characters from a string:

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

string myString = "abcdxxx"; 

Note that the string is dynamic data.

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

10 个解决方案

#1


112  

read last 3 characters from string [Initially asked question]

从字符串中读取最后3个字符[最初提出的问题]

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

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

myString.Substring(str.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 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.

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

myString = myString.Substring(0, str.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)

String.Remove Method (Int32)

String.Remove方法(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

返回一个新字符串,其中当前实例中从指定位置开始并持续到最后一个位置的所有字符都已被删除

#2


15  

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

#3


9  

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"

#4


5  

myString.Remove(myString.Length-3);

#5


4  

string test = "abcdxxx";
test = test.Remove(test.Length - 3);
//output : abcd

#6


3  

str= str.Remove(str.Length - 3);

str = str.Remove(str.Length - 3);

#7


2  

myString.Substring(myString.Length - 3, 3)

Here are examples on substring.>>

以下是子字符串的示例。>>

http://www.dotnetperls.com/substring

http://www.dotnetperls.com/substring

Refer those.

参考那些。

#8


2  

   string myString = "abcdxxx";
   if (myString.Length<3)
      return;
   string newString=myString.Remove(myString.Length - 3, 3);

#9


2  

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);

#10


1  

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开始到结尾的文本

#1


112  

read last 3 characters from string [Initially asked question]

从字符串中读取最后3个字符[最初提出的问题]

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

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

myString.Substring(str.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 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.

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

myString = myString.Substring(0, str.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)

String.Remove Method (Int32)

String.Remove方法(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

返回一个新字符串,其中当前实例中从指定位置开始并持续到最后一个位置的所有字符都已被删除

#2


15  

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

#3


9  

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"

#4


5  

myString.Remove(myString.Length-3);

#5


4  

string test = "abcdxxx";
test = test.Remove(test.Length - 3);
//output : abcd

#6


3  

str= str.Remove(str.Length - 3);

str = str.Remove(str.Length - 3);

#7


2  

myString.Substring(myString.Length - 3, 3)

Here are examples on substring.>>

以下是子字符串的示例。>>

http://www.dotnetperls.com/substring

http://www.dotnetperls.com/substring

Refer those.

参考那些。

#8


2  

   string myString = "abcdxxx";
   if (myString.Length<3)
      return;
   string newString=myString.Remove(myString.Length - 3, 3);

#9


2  

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);

#10


1  

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开始到结尾的文本