如何删除 vb.net 文本框中的最后 5 个或更多或更少的字符

How do I remove the last 5, or more, or less characters in a textbox for vb.net

示例:假设我有一个包含文本 "visual basic" 的文本框,我是否可以删除 "basic" 或者即使我不知道该文本是否可以删除删除文本框中的最后 5 个或更多或更少?

试试这个:

MyString = TextBox1.Text
TextBox1.Text = MyString.Left(MyString.Length - 5)

如果 Left 不起作用,试试这个:

MyString = TextBox1.Text
If MyString.Length < 5 Then
    TextBox1.Text = MyString.SubString(5, str.Length - 5)
End If

如果你想删除最后一个词

Dim words AS String() = YourTextBox.Text.Split(" "c)
YourTextBox.Text = String.Join(" ", words.Take(words - 1))

如果你想删除一些最后的字符

Dim amountToRemove As Integer = 5
YoutTextBox.Text = YoutTextBox.Text.Remove(YoutTextBox.Text.Length - amountToRemove)

或 LINQ 方法

Dim amountOfCharactersToRemove As Integer = 5
Dim amountOfCharactersToTake = YourTextBox.Text.Length - amountOfCharactersToRemove 
Dim characters As Char() = YourTextBox.Text.
                                       ToCharArray().
                                       Take(amountOfCharactersToTake).
                                       ToArray()
YoutTextBox.Text = new string(characters)