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

3.5k Views Asked by At

Example: say i have a text box that has the text "visual basic" is there a way i can remove "basic" or even if i don't know the text is there a way i can remove the last 5 or more or less in the text box?

2

There are 2 best solutions below

2
On BEST ANSWER

Try this:

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

If Left doesn't work, try this:

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

If you want remove last word

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

If you want remove some number of last characters

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

Or LINQ approach

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)