0

In VB.NET, I want to remove an entire string from the end of my variable and not individual characters. What is the best way to do this?

For example, my string text is as follows:
Jack left with 3.00 in his pocket but returned with only 2.00

I want to remove the .00 from the end of the text and not the middle. I want the result to be:
Jack left with 3.00 in his pocket but returned with only 2

If I use TrimEnd with "." and "0", I will get incorrect trims as follows:
Something. -> Something (incorrect)
Anything 0 -> Anything (incorrect)

I want only ".00" to be remove from the end, not ".", ".0", or "0".

Can this be done easily?

Acavier
  • 79
  • 7
  • 1
    `If myString.EndsWith(".00") Then myString = myString.SubString(0, myString.Length - 3)` – Blackwood Nov 20 '19 at 19:25
  • 2
    `Dim poorJack As String = "Jack left with 3.00 in his pocket but returned with only 2.00" poorJack = poorJack.TrimEnd(".00".ToCharArray())` – Jimi Nov 20 '19 at 21:10
  • Blackwood, your comment seems the closest to a correct answer. – Acavier Nov 21 '19 at 00:22

2 Answers2

1

If you want to remove a shorter string from the end of a longer string, You need to first find out if the shorter string actually appears at the end of the longer string. You can use the EndsWith method to do that. If it does appear, you can remove it using the SubString method.

Here is a function that will remove any shorter string from the end of a longer string. If the shorter string does not appear at the end of the longer string, the whole longer string is returned.

Function RemoveFromEnd(fullString As String, removeString As String) As String
    If fullString.EndsWith(removeString) Then Return fullString.Substring(0, fullString.Length - removeString.Length)
    Return fullString
End Function

You could call the function like this:

Dim someString = "Jack left with 3.00 in his pocket but returned with only 2.00"
somestring = RemoveFromEnd(someString, ".00")

This will avoid the two problems you had when using TrimEnd.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
0

I hope this might help you

To remove at END OF YOUR STRING:

Private Function removeFromEnd(completeString As String, searchedString As String)
    If Len(completeString) > 0 Then
        Return completeString.Remove(completeString.LastIndexOf(searchedString))
    End If
    Return ""
End Function

If you want to remove or replace a specific index of your searched string into your string:

    Private Function replaceFromStringInSpecificIndex(completeString As String, searchedString As String, replaceString As String, whichElement As Integer)
        Dim result As String = ""

        If Len(completeString) > 0 Then
            Dim elements() As String = completeString.Replace(searchedString, Char.MinValue).Split(Char.MinValue)
            For i As Integer = 0 To elements.Length - 1
                If Len(elements(i)) > 0 Then
                    result &= elements(i)
                    result &= IIf(i = whichElement, replaceString, searchedString)
                End If
            Next
        End If

        Return result

    End Function

Usage:

To remove at the end of your string:

Dim removedFromEnd As String = removeFromEnd("Jack left with 3.00 in his pocket but returned with only 2.00", ".00")

To remove/replace a specific index of searched string (here third element is replaced with OPSSSSS….)

Dim replacedSpecificIndex As String = replaceFromStringInSpecificIndex("Jack left with 3.00 in his pocket but returned with only 2.00 Or again Jack left with 3.00 in his pocket but returned with only 2.00", ".00", " OPSSSSSSS Replaced ", 2)

To remove a specific number of chars from end of your String:

Private Function removeFromEndXChars(completeString As String, nrChars As Integer)
    If Len(completeString) > nrChars Then
        Return completeString.Remove(completeString.Length - nrChars)
    End If
    Return ""
End Function

Usage:

 Dim sfinal As String = removeFromEndXChars("Jack left with 3.00 in his pocket but returned with only 2.00", ".00".Length)
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
  • `removeFromEnd("ABC00DEF00", "00") = "ABC00DEF"`, but `removeFromEnd("ABC00DEF", "00") = "ABC"`. It doesn't *only* remove it from the end, just starting at the last occurrence which may be in the middle. – djv Nov 20 '19 at 22:14
  • Consider the string `Jack left with 3.00 in his pocket but returned with only 1.99`, which returns `Jack left with 3` – djv Nov 20 '19 at 22:16
  • You are correct, djv. I only want to remove the string if it appears at the end and not just the last occurrence. – Acavier Nov 21 '19 at 00:23
  • Yes. All I read here is right. The answer is based in question if I well understand “want to remove an entire string from the end”. However I’ve update the code to do that for a specific number of chars from end. – G3nt_M3caj Nov 21 '19 at 08:27