How to remove trailing numbers

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1012 Views - Last Post: 08 April 2013 - 07:48 PM Rate Topic: -----

#1 djstanley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

How to remove trailing numbers

Posted 05 April 2013 - 04:49 PM

Hey,

I'm trying to remove all trailing numbers from a string, so for example:

hello123 = hello
hello123there123 = hello123there
12345hello12345 = 12345hello

You get the gist..

Here's my inefficient, and nonworking code:

[color="#0000FF"]            If IsNumeric(nonumbername.length - 1) = True Then


                Do Until IsNumeric(nonumbername.Length - 1) = False
                    If IsNumeric(nonumbername.Length - 1) = True Then
                        nonumbername = nonumbername.Remove(nonumbername.Length - 1)
                    End If
                Loop

                MsgBox(nonumbername)

            End If[/color]


(Don't feel like you need to use this code if you see a simpler way of doing this!)
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to remove trailing numbers

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: How to remove trailing numbers

Posted 05 April 2013 - 05:19 PM

    Sub Main()
        Dim sSample As String = "hello12345"
        Dim sResultR As String = StrReverse(sSample).ToUpper()
        Dim iLast As Integer = sResultR.IndexOfAny("ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray)
        Dim sResultN As String = sSample.Substring(0, sSample.Length - iLast)

        Console.WriteLine(sResultN)

        Console.ReadKey()
    End Sub


or

    Sub Main()
        Dim sSample As String = "hello12345"
        Dim sResultR As String = StrReverse(sSample).ToUpper()
        Dim iLast As Integer = sResultR.IndexOfAny("ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray)
        Dim sResultN As String = String.Empty
        If iLast = -1 Then
            sResultN = ""
        Else
            sResultN = sSample.Substring(0, sSample.Length - iLast)
        End If

        Console.WriteLine(sResultN)

        Console.ReadKey()
    End Sub

(assuming that all characters are alphanumeric)

I'm trusting that this is not an assignment. If it is then you should continue with your current code and learn to debug it - see my signature.

This post has been edited by andrewsw: 05 April 2013 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to remove trailing numbers

Posted 05 April 2013 - 05:27 PM

I'm rather partial to:

        Dim s As String = "1234567890hello123498there176455"

        While IsNumeric(s.Substring(s.Length - 1))
            s = s.Substring(0, s.Length - 1)
            Debug.Print(s)
        End While


Was This Post Helpful? 1
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: How to remove trailing numbers

Posted 05 April 2013 - 05:28 PM

Hi,
Here is another way it could be done also. There is always more than one way. :bigsmile:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String = "Hello123There123"
        For x = str.Length - 1 To 0 Step -1
            If Not IsNumeric(str(x)) Then
                str = str.Remove(x + 1)
                Exit For
            End If
        Next
        MessageBox.Show(str)
    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: How to remove trailing numbers

Posted 05 April 2013 - 05:33 PM

I went for the less obvious, cuter!, approach :)
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: How to remove trailing numbers

Posted 05 April 2013 - 05:34 PM

Manipulate the string only once.
Function StripTrailingDigits(Text As String) As String
  Dim l = Text.Length-1
  While (l>0) AndAlso ("0123456789".Any(Function(d) Text(l)=d))
    l-=1
  End While
  Return If(l>0,Text.SubString(0,l),"")
End Function


This post has been edited by AdamSpeight2008: 05 April 2013 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: How to remove trailing numbers

Posted 05 April 2013 - 05:56 PM

Or more concisely, .net already has this functionality built-in.
    Function StripTrailingDigits(Text As String) As String
      Return  Text.TrimEnd( "0123456789".ToCharArray)
End Function


Was This Post Helpful? 2
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to remove trailing numbers

Posted 05 April 2013 - 07:23 PM

View PostAdamSpeight2008, on 05 April 2013 - 06:56 PM, said:

Or more concisely, .net already has this functionality built-in.
    Function StripTrailingDigits(Text As String) As String
      Return  Text.TrimEnd( "0123456789".ToCharArray)
End Function


Nice! I was going to suggest .TrimEnd, but didn't realize I could use .ToCharArray on a string.
Was This Post Helpful? 1
  • +
  • -

#9 djstanley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: How to remove trailing numbers

Posted 06 April 2013 - 02:54 AM

Thank you very much everybody! All great responses that all work brilliantly in their own right.

lar3ry I ended up using the first of your examples, it works great.

Thankyou all once again :)
DJS
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to remove trailing numbers

Posted 06 April 2013 - 12:15 PM

View Postdjstanley, on 06 April 2013 - 03:54 AM, said:

Thank you very much everybody! All great responses that all work brilliantly in their own right.

lar3ry I ended up using the first of your examples, it works great.

I'me rather surprised you didn't use AdamSpeight2008's example, as it's not only shorter, but probably executes more quickly (but not by a large margin).

If you avoided it because it's in a Function, there's really no need, as it can be used in-line in your other code. Instead of using it in a Function, and using a Return to assign it to a variable, you can do it directly:

     Text = Text.TrimEnd( "0123456789".ToCharArray)


If you are avoiding it becuase you don't really understand it, here's how it works..

It converts the literal String to a character array, each entry of which contains a single digit in Char form. Then it uses the built-in String.TrimEnd to remove all ending characters that match the characters in the array, and finally, assigns the modified string to itself (though is could be assigned to any other String variable).

This post has been edited by lar3ry: 06 April 2013 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: How to remove trailing numbers

Posted 06 April 2013 - 12:56 PM

It is easy to see the actual implementation if you download and use and IL Decompiler like
IL Spy
or
enable .net framework debugging.

This post has been edited by AdamSpeight2008: 06 April 2013 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to remove trailing numbers

Posted 06 April 2013 - 03:06 PM

View PostAdamSpeight2008, on 06 April 2013 - 01:56 PM, said:

It is easy to see the actual implementation if you download and use and IL Decompiler like
IL Spy
or
enable .net framework debugging.

Cool! I could not get the ".Net enable framework debugging" working, but I did download ILSpy, and it's a great little utility. I see the implementation was not much different than my suggestion.

However, a timing test shows that the String.TrimEnd is about 3 times faster, which isn't a whole lot... about 30 vs. 10 ms for 100,000 iterations, on my machine...

    Dim s As String = "1234567890hello123498there176455"
    Dim sw As New Stopwatch
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        sw.Restart()
        For i = 0 To 100000
            While IsNumeric(s.Substring(s.Length - 1))
                s = s.Substring(0, s.Length - 1)
            End While
        Next
        Debug.Print(sw.Elapsed.TotalMilliseconds.ToString)
        sw.Restart()
        For i = 1 To 100000
            s = s.TrimEnd("0123456789".ToCharArray)
        Next
        Debug.Print(sw.Elapsed.TotalMilliseconds.ToString)
    End Sub


Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: How to remove trailing numbers

Posted 06 April 2013 - 03:37 PM

There is always regex (for completeness :))

        Dim sTest As String = "hello12345"
        Dim sTrim As String = Regex.Replace(sTest, "[0-9]+$", "")

        MessageBox.Show(sTrim)

but it is probably not worth using regex for something this simple.
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,469
  • Joined: 29-May 08

Re: How to remove trailing numbers

Posted 06 April 2013 - 03:53 PM

you are repeatedly creating a new string instance for each instance.
While IsNumeric(s.Substring(s.Length - 1))
 s= s.Substring(0, s.Length - 1)
End While



My method and the .net version don't do that, they just create the final string once.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: How to remove trailing numbers

Posted 06 April 2013 - 04:06 PM

View PostAdamSpeight2008, on 06 April 2013 - 10:53 PM, said:

you are repeatedly creating a new string instance for each instance.
While IsNumeric(s.Substring(s.Length - 1))
 s= s.Substring(0, s.Length - 1)
End While



My method and the .net version don't do that, they just create the final string once.

Yes, there is no need to keep creating new strings, just use a counter:

        Dim sTest As String = "hello12345"
        Dim i As Integer = sTest.Length - 1
        While i > 0 And IsNumeric(sTest(i))
            i -= 1
        End While

        MessageBox.Show(sTest.Substring(0, i + 1))

(should check for an empty string first though)

This post has been edited by andrewsw: 06 April 2013 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2