How to extract the message exactly in vb.net - vb.net

Below is my textbox which i store the output of my serial
Below is where i put the message inside the DataGridView the problem lies here see the last row of the grid there is an OK text.
my last photo shows the value of my line of text.
Dim LineOfText As String
Dim i As Integer
Dim aryTextFile() As String
LineOfText = tt.Text
aryTextFile = Split(LineOfText, "+CMGL", , CompareMethod.Text)
dgv.Rows.Clear()
For i = 1 To UBound(aryTextFile)
'**********************
Dim LineOfTexts As String
Dim aryTextFiles() As String
LineOfTexts = aryTextFile(i)
aryTextFiles = Split(LineOfTexts, """", , CompareMethod.Text)
aryTextFiles(5) = aryTextFiles(5).Substring(0, 17).Replace(",", "-")
dgv.Rows.Add(New String() {aryTextFiles(3), aryTextFiles(6), aryTextFiles(5)})
Next i
is there any other way to extract the message exactly?
aryTextFiles(3) is the phone number/Sender aryTextFiles(6) is the message and aryTextFiles(5) is the date and time.

You can remove the last word by using Trim()
LineOfText = tt.Text.Substring(0, tt.Text.LastIndexOf(vbCrLf) - 3).Trim()
I used -3 to trim the last 3 characters.

Related

VB.net Read Specific Lines From a Text File That Start With and Stop Reading When Start With

I'm looking to read lines from a text file that start with certain characters and stop when the line starts with other characters. So in my example I would like to start reading at line AB and stop at line EF however not all lines will contain the CD line. There will always be a AB line and EF line, however the number of lines in between is unknown.
Here is an example of the lines in a text file I would be reading. You can see that this will create two rows in the DataGridView however the first row is missing the CD line and should be blank.
AB-id1
EF-address1
AB-id2
CD-name1
EF-address2
Here is the code I have so far:
Dim lines() As String = File.ReadAllLines(textfile)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("AB") Then
Dim nextLines As String() = lines.Skip(i + 1).ToArray
Dim info As String = nextLines.FirstOrDefault(Function(Line) Line.StartsWith("CD"))
Dim name As String = "Yes"
Dim info2 As String = nextLines.FirstOrDefault(Function(Line) Line.StartsWith("EF"))
Dim address As String = "Yes"
End If
DataGridView.Rows.Add(name,address)
Next
Now the output I currently get is:
|Yes|Yes|
|Yes|Yes|
And I should be getting:
||Yes|
|Yes|Yes|
It looks like it's reading too far down the text file and I need it to stop reading at EF. I've tried Do while and Do Until with no success. Any suggestions?
You could use the Array.FindIndex function to get the index of the next line starting with your prefix. This way you don't have to skip lines and create a new array each time.
Try this out instead:
Dim lines() As String = File.ReadAllLines(textFile)
For i As Integer = 0 To lines.Length - 1
If lines(i).StartsWith("AB") Then
Dim addressIndex As Integer = Array.FindIndex(lines, i + 1, Function(Line) Line.StartsWith("EF"))
Dim address As String = If(addressIndex <> -1, lines(addressIndex).Substring(3), "") ' Get everything past the "-"
Dim name As String = ""
If addressIndex <> -1 Then
Dim nameIndex As Integer = Array.FindIndex(lines, i + 1, addressIndex - i, Function(line) line.StartsWith("CD"))
If nameIndex <> -1 Then
name = lines(nameIndex).Substring(3) ' Get everything past the "-"
End If
End If
DataGridView.Rows.Add(name, address)
End If
Next

What is the simplest way to get the second item of each row in a string file

I have a String file with 8 items (separated by commas) in each row, e.g., CA,23456,aName,aType,anotherName,aWord,secondword,number. I want to create a new string of items consisting of the 2nd item (an Integer) of each row of the original file. I know there are many ways to do this but someone out there knows how to do it with very few lines of code, which is what I am looking for. I prefer not to use a parser.
The way to show what I have tried is to look at the code below.
Dim sn2 As String = ""
Dim sn2S As String = ""
Using readFile As New StreamReader(newFile1)
Do While readFile.Peek() <> -1
sn2S = readFile.ReadLine(1)
sn2 = sn2 & sn2S & ","
Loop
End Using
The code returns the second character of each row not the second item. What I hope to get is a string that looks like: 123,1345,4325,3321,3456,3211 etc. Where each number is the second item in each row of the original file.
You could split it up by cells
Dim row As String = "CA,23456,aName,aType,anotherName,aWord,secondword,number"
Dim cells() As String = row.Split(",")
Dim cellValue As String = cells(1)
But in your case, I would just do a search and Substring by the index of the delimiter.
Dim startPosition As Integer = row.IndexOf(",") + 1
Dim endPosition As Integer = row.IndexOf(",", startPosition)
Dim cellValue As String = row.Substring(startPosition, endPosition - startPosition)
If you have the whole file in memory, there could be some regex that could do the job with one pass.
As for this line
sn2 = sn2 & sn2S & ","
You might want to check at doing a join or using stringbuilder.
You could try
Dim sn2 As String = ""
Dim sn2S(7) As String = ""
Using readFile As New StreamReader(newFile1)
Do While readFile.Peek() <> -1
Array.Clear(sn25,0,sn25.Length)
sn2S = readFile.ReadLine(1).Split(",")
sn2 = sn2 & sn2S(1) & ","
Loop
End Using
In one line
Dim sn2 = String.Join(",", File.ReadAllLines(newFile1).Select(Function(s) s.Split(","c)(1)))
From the inside-out:
File.ReadAllLines(newFile1) splits the file into lines and results in a string array holding those lines, which is fed into...
...Select(Function(s) s.Split(","c)(1)) which operates on each line by splitting the line by comma s.Split(","c) and then indexing the resulting array (1) to return the second (zero-based) element. This is fed into...
String.Join(",", ... ) which takes those second elements and joins then together with comma.

Input string was not in a correct format while adding string to integer

I have a code like this :
Dim mincatval As String
Dim strarr() As String = dr1(0).ToString().Split(New Char() {"-"c})
Dim i As String
i = (Integer.Parse(strarr(0)) + 1)
mincatval = i
my dr(1) value is L1 i want to add 1,so i want the out put L2,but i am getting error like this :Input string was not in a correct format.
Supposing that strarr(0) is the word "L1" and you want it to become "L2" then you need to isolate the numeric part from the text part and then rebuild the string taking the first part of strarr and the incremented value
Dim mincatval As String
Dim strarr() As String = dr1(0).ToString().Split(New Char() {"-"c})
Dim i As String
Dim itShouldBeAnumber = strarr(0).Substring(1)
if Int32.TryParse(itShouldBeAnumber, i) Then
mincatval = strarr(0).Substring(0,1) & (i + 1)
else
MessageBox.Show("Not a valid number from position 1 of " & strarr(0))
End if
Of course this solution assumes that your string is Always composed of an initial letter followed by a numeric value that could be interpreted as an integer

Vb.net - Split String and convert to DateTime

I have 1 datagridview named IncomingMailDGV.
Sample data on Contents Column:
R-1 temperature (6.25) rose above high bound SLTCR at Warehouse-1 at 27/07/15 13:40
R-2 temperature (6.62) rose above high bound SLTCR at Store at 27/07/15 13:42
R-3 temperature (6.31) rose above high bound SLTCR at Warehouse-2 at 27/07/15 13:45
Note: Some contents has three spaces on the last part of the message.
I want to get the date and time which is placed on the last part and convert it to this format: "M/dd/yyyy HH:mm tt"
I'm using below code but its not working:
Sub FilterAlert()
Try
Dim Dbcon As New OleDbConnection(connStr)
Dbcon.Open()
For x As Integer = 0 To IncomingMailDGV.Rows.Count - 1
Dim line2 As String = IncomingMailDGV.Rows(x).Cells("Contents").Value.ToString
Dim separators() As String = {",", " ", "(", ")"}
Dim length As Integer = line2.Length
Dim alertdatetime As String
Dim alertdate_time As String
data2 = line2.Split(separators, StringSplitOptions.RemoveEmptyEntries)
alertdate_time = line2.Substring(length - 14, 14)
Dim alertstring = alertdate_time.Split(separators, StringSplitOptions.RemoveEmptyEntries)
alertdatetime = alertstring(0)
alertdatetime = DateTime.ParseExact(alertdatetime, "dd/MM/yyHH:mm", Nothing)
console.writeline (alertdatetime)
Next
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
Update: Some cells on Contents Column are empty.
If you mean that there may be more than one space between the date and the time, then you can just take the last 14 characters from the string. The following code assumes that the date and time are always preceded by " at ". It's not clear what you want to do if the string is not in the correct format, this code simply ignores invalid strings.
Note that DateTime doesn't have a format, it's just numbers. You can use DateTime.ToString to output the date and time in whatever format you want.
This code is intended to replace everything inside the For loop.
Dim line2 As String = IncomingMailDGV.Rows(x).Cells("Contents").Value.ToString
Dim pos As Integer = line2.LastIndexOf(" at ")
If pos < 0 Then Continue For
Dim alertstring() As String = line2.Substring(pos + 4).Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
If alertstring.length <> 2 Then Continue For
Dim alertdatetime As DateTime
If Not DateTime.TryParseExact(alertstring(0) & alertstring(1), "dd/MM/yyHH:mm", _
System.Globalization.CultureInfo.InvariantCulture, Nothing, alertdatetime) Then Continue For
Console.WriteLine(alertdatetime.ToString("M/dd/yyyy HH:mm tt"))

How do I manipulate the last string of a Richtextbox in Visual Basic

I am trying to take a string in a rich text box and replace them with a different string.
Now how this should work is that if two same characters are entered into the text box
e.g tt the "tt" will be replaced with "Ǿt" , it adds back one of the t's to the replaced string. Only the most recently entered string is manipulated if two same characters are entered .
I read the LAST string that is in the RichTextBox by using this method
Dim laststring As String = RichTextBox1.Text.Split(" ").Last
'hitting space bar breaks the operation so if i enter t t there will be no replacement
this is the replacement method which I use , it works correctly .
if laststring = "tt"
RichTextBox1 .Text = RichTextBox1 .Text.Replace("tt", "Ǿt")
This method is inefficient because i need to check id there are double letters for all letters and if i was to use this method it would tavke up a lot of code .
how can I accomplish this using a shorter method??
You need to put the if then section in a loop.
Dim holdstring As String
Dim doubleinstance() As String = {"bb", "tt", "uu"} ' array
Dim curstring As String = RichTextBox1.Text.Split(" ").Last
For Each item As String In doubleinstance
If RichTextBox1.Text.EndsWith(item) Then
holdstring = RichTextBox1.Text.Split(" ").Last.Length - 1 ' change to subtract 1 character from doubleinstance
RichTextBox1.Text = RichTextBox1.Text.Replace(curstring, "Ǿt" & holdstring)
MsgBox(curstring)
End If
Next item
Here's a bit of code to get you in the right direction...
There are a couple of variations of .Find, but you probably want to look at the .Select method.
With RichTextBox1
.Find("Don")
.SelectedText = "Mr. Awesome"
End With
Here is a way I came up with
Dim holdstring As String
Dim doubleinstance() As String = {"bb", "tt", "uu"} ' array
Dim curstring As String = RichTextBox1.Text.Split(" ").Last
If curstring = doubleinstance(0) And RichTextBox1.Text.EndsWith(doubleinstance(0)) Then
holdstring = RichTextBox1.Text.Split(" ").Last.Length - 1 ' change to subtract 1 character from doubleinstance
RichTextBox1.Text = RichTextBox1.Text.Replace(curstring, "Ǿt" + holdstring)
MsgBox(curstring)
End If
where i have doubleinstance(0) how do i get the if statement to not only check a single index but all of the index from 0 to 2 in this example ?