6 Replies - 269 Views - Last Post: 29 January 2013 - 11:53 AM Rate Topic: -----

#1 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 166
  • Joined: 18-October 12

Looking for a better answer again

Posted 29 January 2013 - 10:32 AM

Hello folks, I've got a problem here where I need to take a number stored as a string and split the digits when the string length is two (i.e 10 becomes 1 0) or if the length is one, I need to add a tab and a 0 to the number (i.e. 1 also becomes 1 0). I wrote a quick and dirty program to accomplish this, but my GOD is it slow. Surely there's a faster way to process simple text than this:

    Private Sub txtInput_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.DoubleClick
        txtInput.Text = Clipboard.GetText
        For i = 0 To txtInput.Lines.Count - 1
            If Len(txtInput.Lines(i)) = 1 Then 
                txtOutput.Text = txtOutput.Text & txtInput.Lines(i).Substring(0, 1) & vbTab & "0" & vbNewLine
            ElseIf Len(txtInput.Lines(i)) = 2 Then
                txtOutput.Text = txtOutput.Text & txtInput.Lines(i).Substring(0, 1) & vbTab & txtInput.Lines(i).Substring(1, 1) & vbNewLine
            Else
                'Do nothing (don't want to output blank lines or lines longer than 2)
            End If
            Me.Text = "Processing Line: " & i
        Next
        txtInput.SelectAll()
        Clipboard.SetText(txtOutput.Text)
    End Sub


This post has been edited by C.Andrews: 29 January 2013 - 10:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Looking for a better answer again

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3219
  • View blog
  • Posts: 10,790
  • Joined: 12-December 12

Re: Looking for a better answer again

Posted 29 January 2013 - 10:46 AM

When you do this:
txtOutput.Text = txtOutput.Text & 

you are updating the form, character by character - and it will be slow and possibly flash the screen.

Gather the text you need in a separate string and update the text once, after the loop has completed.

This line:
Me.Text = "Processing Line: " & i

will also be slowing it down; try commenting it out to see what impact it has.

You could investigate a ProgressBar as an alternative.

This post has been edited by andrewsw: 29 January 2013 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 ybadragon  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 178
  • View blog
  • Posts: 1,111
  • Joined: 11-May 12

Re: Looking for a better answer again

Posted 29 January 2013 - 10:51 AM

I'm actually doing this kind of thing at work currently, and if you look into "PadLeft" and "PadRight" methods it should get you going in the right direction for adding the 0's to the beginning or end of the String
Was This Post Helpful? 0
  • +
  • -

#4 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 166
  • Joined: 18-October 12

Re: Looking for a better answer again

Posted 29 January 2013 - 11:15 AM

View Postandrewsw, on 29 January 2013 - 10:46 AM, said:

When you do this:
txtOutput.Text = txtOutput.Text & 

you are updating the form, character by character - and it will be slow and possibly flash the screen.

Gather the text you need in a separate string and update the text once, after the loop has completed.


I thought of that solution but I'm having trouble implementing it. There doesn't seem to be a direct way to transfer clipboard data into a string array. I tried simply pasting the clipboard into the textbox, then looping through each line in the textbox and adding them to a list object; however it seems looping through the textbox is still pretty damn slow even when I'm not updating anything but my list.

If I don't have my data in an array of some kind, I'm unclear on how to process each line. If I store everything as one string, I can't loop through it, right?

    Private Sub txtInput_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.DoubleClick
        Dim strInput As String = ""
        txtInput.Text = Clipboard.GetText
        For i = 0 To txtInput.Lines.Count - 1
            If Len(txtInput.Lines(i)) = 1 Then
                strInput = strInput & txtInput.Lines(i).Substring(0, 1) & vbTab & "0" & vbNewLine
            ElseIf Len(txtInput.Lines(i)) = 2 Then
                strInput = strInput & txtInput.Lines(i).Substring(0, 1) & vbTab & txtInput.Lines(i).Substring(1, 1) & vbNewLine
            Else
                'Do nothing (don't want to output blank lines or lines longer than 2)
            End If
            'Me.Text = "Processing Line: " & i
        Next
        Clipboard.SetText(strInput)
    End Sub



View Postybadragon, on 29 January 2013 - 10:51 AM, said:

I'm actually doing this kind of thing at work currently, and if you look into "PadLeft" and "PadRight" methods it should get you going in the right direction for adding the 0's to the beginning or end of the String


I appreciate the tip, but unfortunately I'm doing more than just adding a 0 to one side or the other. If that were the case I'd just use Excel and add another column. Unless PadLeft and PadRight offer some method of splitting a 2-digit string down the middle. Maybe I better look in to it.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3219
  • View blog
  • Posts: 10,790
  • Joined: 12-December 12

Re: Looking for a better answer again

Posted 29 January 2013 - 11:27 AM

Quote

There doesn't seem to be a direct way to transfer clipboard data into a string array.


        Dim strClip As String = Clipboard.GetText()
        Dim strClipArray() = strClip.Split(vbNewLine)

        For x As Integer = 0 To strClipArray.Length - 1
            MsgBox(strClipArray(x))
        Next


Added: In response to your other question, you can loop through a single string but you would be doing it character by character.

This post has been edited by andrewsw: 29 January 2013 - 11:31 AM

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3219
  • View blog
  • Posts: 10,790
  • Joined: 12-December 12

Re: Looking for a better answer again

Posted 29 January 2013 - 11:35 AM

Dim strWibble As String = "Here be wibble"
For Each ch As Char In strWibble
    Console.Write(ch & ", ")
next ch


Output:

Quote

'H, e, r, e, , b, e, , w, i, b, b, l, e,

Was This Post Helpful? 0
  • +
  • -

#7 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 166
  • Joined: 18-October 12

Re: Looking for a better answer again

Posted 29 January 2013 - 11:53 AM

View Postandrewsw, on 29 January 2013 - 11:27 AM, said:

Quote

There doesn't seem to be a direct way to transfer clipboard data into a string array.


        Dim strClip As String = Clipboard.GetText()
        Dim strClipArray() = strClip.Split(vbNewLine)

        For x As Integer = 0 To strClipArray.Length - 1
            MsgBox(strClipArray(x))
        Next


Added: In response to your other question, you can loop through a single string but you would be doing it character by character.


Oh. Holy crap. It never crossed my mind that I could split by vbNewLine. Thanks, once again, for the tip.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1