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
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.
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.
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
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....
Re: How to find specific characters(items) in string array(or list) and then merge th
Originally Posted by GremlinSA
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.
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. =)
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.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.