How to find specific characters(items) in string array(or list) and then merge them?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: How to find specific characters(items) in string array(or list) and then merge them?

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    Prizren
    Posts
    37

    Post How to find specific characters(items) in string array(or list) and then merge them?

    I'm having a problem recently, a logical issue so hard to determine various examples.
    I need the text that is inserted in textbox, which has been altered or modified or twisted to have a proper format through line accordance and constant white spaces etc. And some cases I stuck at defining the If statement within the loop. Here's an example of a text I'm trying to reformat
    3/7/ 12 3:50PM 108 02 6060001108 00:00'48" .. .. 3 / 7
    /12 3:51PM 102 01 00 49 17
    34973369 00:00'33" ....
    3/7/12 3:51P M 1
    02 02 00491734973369 00:00'43" ....
    3/ 7/12 3:54PM 102 02 00491734973369
    00:00'2 1" ....
    3/7/12 3: 55PM 102 02 044659912
    00:00'24" ....
    and the way it should look like is :
    3/7/12 3:50PM 108 02 6060001108 00:00'48" ....
    3/7/12 3:51PM 102 01 00491734973369 00:00'33" ....
    3/7/12 3:51PM 102 02 00491734973369 00:00'43" ....
    3/7/12 3:54PM 102 02 00491734973369 00:00'21" ....
    3/7/12 3:55PM 102 02 044659912 00:00'24" ....
    And here's the code that I filter the message in the textbox.
    Code:
     Dim fjalet As String = txtReceive.Text
                Dim status As Boolean
                Dim strfilter As String = ""
                Dim oldfilter As String = ""
                For Each b As Char In fjalet
                    If status = True Then
                        If b <> oldfilter Then
                            status = False
                            strfilter = strfilter + b
                        End If
                    Else
                        strfilter = strfilter + b
                    End If
                    If b = " " Then
                        status = True
                    End If
                    oldfilter = b
                Next
                'Since the bytes that are converted after they're received in the Data Received event have white spaces
                'and when trying to split them into smaller parts of that string, resulsts some logical errors by
                'inserting each of the characters into designated columns of the database table thus to prevent that
                'a replacement of every white space next to randomly selected character with the sole character is a must.
    
                strfilter = strfilter.Replace(": ", ":")
                strfilter = strfilter.Replace("/ ", "/")
                strfilter = strfilter.Replace(" /", "/")
                strfilter = strfilter.Replace("< ", "<")
                strfilter = strfilter.Replace("A ", "A")
                strfilter = strfilter.Replace(" i", "i")
                strfilter = strfilter.Replace(" >", ">")
                strfilter = strfilter.Replace(". 0", ".0")
                strfilter = strfilter.Replace(". .", "..")
                strfilter = strfilter.Replace("....", ".... ")
                strfilter = strfilter.Replace(vbCrLf + "/", "/")
                strfilter = strfilter.Replace("* ", "*")
                strfilter = strfilter.Replace("-", "")
                strfilter = strfilter.Replace("P ", "P")
                strfilter = strfilter.Replace(" M", "M")
                strfilter = strfilter.Replace("Date", "")
                strfilter = strfilter.Replace("D ate", "")
                strfilter = strfilter.Replace("Time", "")
                strfilter = strfilter.Replace("Ext.", "")
                strfilter = strfilter.Replace("CO", "")
                strfilter = strfilter.Replace("Dial", "")
                strfilter = strfilter.Replace("number", "")
                strfilter = strfilter.Replace("Duration", "")
                strfilter = strfilter.Replace("Code", "")
    
                Dim split2 As String() = strfilter.Split(New [Char]() {" "c, CChar(vbTab), CChar(vbCrLf), CChar(vbCr), CChar(vbLf)})
                Dim listfilter As New List(Of String)(split2)
                Dim i As Integer = 0
                Dim itemnew As String = ""
                For Each item As String In split2
                    If item.Trim = "" Then
                        listfilter.RemoveAt(i)                   
                        i -= 1
                    End If
                    i += 1
                Next
    I'm using Microsoft Visual Studio 2010 Ultimate version with Microsoft SQL Server Management Studio. This code is being fired on mouse click event of a button control. This is a snippet of my code there are more filtering after but here is what should be changed.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: How to find specific characters(items) in string array(or list) and then merge th

    Could you post the data received event from your comm control. It looks like the issue may be with how you are getting the data in. I doubt the smdr is coping across like you posted above, so I am thinking you might be getting some extra data in your receive event.

  3. #3
    Join Date
    Feb 2012
    Location
    Prizren
    Posts
    37

    Re: How to find specific characters(items) in string array(or list) and then merge th

    Here it is.
    Code:
     Dim n As Integer = srpPBX.BytesToRead() 'Declare n as Integer, system-defined class type variable and set it's value to the bytes-to-be-read from the serial port srtPBX.
            Dim comBuffer(n - 1) As Byte 'Declare comBuffer as Byte, system-defined class type array set to the sum of bytes-read minus one, since bytes don't start to count from 0.
            Dim BytesReceived As Integer = 0 'Declare BytesReceived as Integer, system-defined class type variable and set to 0. This is a counter for the bytes-received.
    
            Try
                BytesReceived = srpPBX.Read(comBuffer, 0, n) 'Set BytesReceived to bytes read from n , starting from 0 and storing them into comBuffer.
                If BytesReceived > 0 Then 'If BytesReceived is bigger than 0 then...
    
                    x = System.Text.Encoding.ASCII.GetString(comBuffer) 'Convert the received bytes by ASCII format-encoding and set it to x string.
    
                End If
                Me.Invoke(New MethodInvoker(AddressOf ReceivedText)) 'Since the control that is needed to operate is declared in another thread an invoke is necessary here with address to ReceivedText.
    
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error) 'Catch exception/s that might occurr anytime by any reason into a simplified message box followed by a critical sign within.
            End Try
    I doubt that something is wrong in here.

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: How to find specific characters(items) in string array(or list) and then merge th

    i'm willing to bet.... The sub here is receiving small chunks of the data and further up stream you accidentally adding in the additional line breaks between each chunk of data....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: How to find specific characters(items) in string array(or list) and then merge th

    Quote Originally Posted by GremlinSA View Post
    i'm willing to bet.... The sub here is receiving small chunks of the data and further up stream you accidentally adding in the additional line breaks between each chunk of data....
    Agreed. Do you do anything to the data in the "ReceivedText" function? Or is it just for putting the text in the textbox. Could you post that function as well.
    Also, if you do do any formatting, could you remove that formatting, and then post the text that comes out of the SMDR.

  6. #6
    Join Date
    Feb 2012
    Location
    Prizren
    Posts
    37

    Re: How to find specific characters(items) in string array(or list) and then merge th

    Actually I do format the text that comes from the DataReceviedEvent.
    I can put the function but its a bit long in case you would spend time reading and analyzing ill post it in here afterwards but im uploading the text file that I didnt touch anything just inserted the message from the DataReceivedEvent directly to the textbox and then saved it to the txt file. I can 100% assure you it's malware-free. I don't have intentions to infect somebody's PC or platform. =)
    Attached Files Attached Files

  7. #7
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: How to find specific characters(items) in string array(or list) and then merge th

    A very bizarre text file you have there. Either the programmers sending out the smdr are smoking some really good stuff, or you are not getting the data properly. So where is it going wrong? It could be you have the comm port set up improperly, or it is happening in the byte to ASCII conversion. TextBoxes do not play nice with a lot of ASCII characters, so I would say, our first step to diagnose this is to stop writing out to a textbox and just start building a large character array, and doing the processing from there. Would it be possible for you to write the data directly to a file instead of trying to put it into a textbox? So open a file before you open the comm port, then instead of trying to put anything in the textbox, just write the data in the "commBuffer" to the file. Then post that file so we can see it.

    If that file is still wacky looking, then it probably has something to do with the comm port setup and not the Ascii conversion.

  8. #8
    Join Date
    Feb 2012
    Location
    Prizren
    Posts
    37

    Re: How to find specific characters(items) in string array(or list) and then merge th

    So you mean forget about ASCII conversion just make a WriteToFile directly after receiving those bytes and store them directly to that file? Ok that is possible for me to do but gotta wait for tomorrow since the PBX machine is located on my work PC and beside it's Sunday today that means no call made today to or from my workplace which results no record in the PBX itself.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center