How to remove trailing numbers

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 04:18 PM

View PostAdamSpeight2008, on 06 April 2013 - 04: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.

While I haven't "looked under the hood" of the assignment, I wonder if that isn't the case. The reason I suspect that a new string is not being created, but perhaps just terminated (as in C), or a count changed, as in a BString, is that changing to the following code makes the times more widely different. The following code, on my machine, gives times of about 36 vs. 10 ms, as opposed to the previous 30 vs. 10 ms.

    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
        Dim idx As Integer = s.Length - 1
        sw.Restart()
        For i = 0 To 100000
            While IsNumeric(s.Substring(idx))  ' same result using (idx,1) for SubString args.
                idx -= 1
            End While
            s = s.Substring(0, idx + 1)
        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
  • +
  • -

#17 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3818
  • View blog
  • Posts: 13,534
  • Joined: 12-December 12

Re: How to remove trailing numbers

Posted 06 April 2013 - 04:33 PM

s.Substring(idx)

wouldn't indexing the string directly, rather than using a function call, be (slightly :)) more efficient:

s(idx)


..and shouldn't a more accurate reflection of the timings include reading the length of the string; well, this is probably infinitesimal.

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

Was This Post Helpful? 0
  • +
  • -

#18 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to remove trailing numbers

Posted 06 April 2013 - 05:40 PM

View Postlar3ry, on 07 April 2013 - 12:18 AM, said:

While I haven't "looked under the hood" of the assignment, I wonder if that isn't the case.
The reason I suspect that a new string is not being created

Strings in .net are Immutable, thus it has to create a new instance. The function has no knowledge of where it's result is being assigned to.

Quote

    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
        Dim idx As Integer = s.Length - 1
        sw.Restart()
        For i = 0 To 100000
            While IsNumeric(s.Substring(idx))  ' same result using (idx,1) for SubString args.
                idx -= 1
            End While
            s = s.Substring(0, idx + 1)
        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


It would help if your tests are on the same string value, but they ain't.
Was This Post Helpful? 0
  • +
  • -

#19 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 - 05:40 PM

View Postandrewsw, on 06 April 2013 - 05:33 PM, said:

s.Substring(idx)

wouldn't indexing the string directly, rather than using a function call, be (slightly :)/>/>) more efficient:

s(idx)


..and shouldn't a more accurate reflection of the timings include reading the length of the string; well, this is probably infinitesimal.

Strangely enough, changing the While line to While IsNumeric(s(idx)) makes it take a millisecond or two longer (37-38 ms).

Without actually looking at the generated code, it's hard to say what's really happening. Compilers have been getting better and better, and often there are optimizations happening that are not intuitively obvious to us.

Including the Dim and assignment of the string length increases the time by perhaps 1 ms in 100,000 iterations.

While all this is fun, and I enjoy it immensely, being a long-time asembler coder, I am VERY glad to see what efficient programs are now being produced by the .Net languages. They are certainly a LOT easier to code in than ANY assembler.
Was This Post Helpful? 0
  • +
  • -

#20 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to remove trailing numbers

Posted 07 April 2013 - 08:22 PM

        Dim x As New StringBuilder("123hello2343580 98766there0918763567348")
        For i As Integer = 0 To 9
            x.Replace(Chr(i + 48), "")
        Next
        Debug.Print(x.ToString)

:)
Was This Post Helpful? 0
  • +
  • -

#21 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: How to remove trailing numbers

Posted 07 April 2013 - 08:27 PM

BobRodes:- The result should be 123hello2343580 98766there.
Was This Post Helpful? 0
  • +
  • -

#22 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: How to remove trailing numbers

Posted 08 April 2013 - 07:48 PM

Well, yes, that's true, in which case yours is the simplest solution. I was experimenting with a way to remove all the digits, as I believe some of the other solutions do.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2