In my application, I need to Read delimited text file in vb.net
2016/05/15 21:59:13,739 [7] INFO - Login.User_Aut - o03dx1n Unknown -
Login: KST028
with:
Day 2016/05/15
Time 21:59:13,739
LogType [7] INFO
MethodName Login.User_Aut
SessionID o03dx1n Unknown
LoginID Login: KST028
Message
Here is my code so far.
OpenFileDialog1.ShowDialog()
Dim filepath As String = OpenFileDialog1.FileName
Dim inputstream As New IO.StreamReader(filepath)
Dim newstr() As String
Dim Day As String
Dim Time As String
Dim LogType As String
Dim MethodName As String
Dim SessionID As String
Dim LoginID As String
Dim Message As String
Do While inputstream.Peek <> -1
newstr = inputstream.ReadLine().Split(" ")
Day = newstr(0)
Time = newstr(1)
LogType = newstr(2)
MethodName = newstr(3)
SessionID = newstr(4)
LoginID = newstr(5)
Message = newstr(6)
Me.LogListView.Items.Add(Day)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(Time)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(LogType)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(MethodName)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(SessionID)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(LoginID)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(Message)
Loop
inputstream.Close()
End Sub
this code is wrong because :
day and time is correct but logtype,session id,loginid,message of columns is wrong
Here is how I would break this down using the hyphen as the delimiter. By doing this you recieve 4 parts (5 if message is included and separated by a hyphen)
the 0th element of the array contains everything up to the first hyphen and needs to be broken down to use each of its parts separately. I've commented the code which should help you understand better what each section is doing.
'these would need adjusted to fit your stream
Dim inputstream As String = "2016/05/15 21:59:13,739 [7] INFO - Login.User_Aut - o03dx1n Unknown - Login: KST028"
'this produces an array of 4 elements
Dim parts() As String = inputstream.Split("-"c)
'element 0 of the array needs to be broken down into its
'proper substrings
Day = parts(0).Substring(0, 10).Trim
Time = parts(0).Substring(12, parts(0).IndexOf("[") - 12).Trim
LogType = parts(0).Substring(parts(0).IndexOf("[") - 1).Trim
'these 3 elements of the array are retrieved fromt the split on the delimiter
MethodName = parts(1).Trim
SessionID = parts(2).Trim
LoginID = parts(3).Trim
'display in console window. Here you would assign these variables to
'your listview instead.
Debug.Print(String.Format("Day={0}{7}Time={1}{7}LogType={2}{7}Method={3}{7}Session={4}{7}Login={5}{7}Message={6}", Day, Time, LogType, MethodName, SessionID, LoginID, Message, Environment.NewLine))
End Sub
Again, this doesn't really handle Message as your string didn't contain one but from this you should be able to work out how to manage it. I wasn't sure if message was something from the string or something entered later
Related
I have a string with following format
Zone: 1 Events: 3
Zone: 2 Events: 7
i am parsing the file with following code
Dim strarr() As String
strarr = Str.Split("/n")
for i = 0 to 2
dim strarrNew() as string
strarrNew = strarr(i)Split(" ")
use value strarrNew(0)
use value strarrNew(1)
next
and i am using values directly but if their is a mistake in the string input
Zone:1Events: 3
spaces are missing than code will not work.
What would be the best way to parse file.
You can adopt this solution only if all the inputs from the file is of the same format:
For Each line As String In File.ReadAllLines("F:\sample.txt")
Dim strarrNew() As String = line.Split(":"c.ToCharArray())
Dim zone As Integer = CInt(Val(strarrNew(1)))
Dim events As Integer = CInt(Val(strarrNew(2)))
'Perform your operations
Next
I am developing VB.NET windows app. in VS 2010.
I want to get the substring
$CostCenterId|4^10
from the below string .
PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian
Rupee^$CostCenterId|4^10$LedgerId|2^3$
The position of current string ($CostCenterId|4^10) in the sequence may be change.
but it will always between the two $ sign.
I have written the below code, but confused abt what to write next ?
Public Sub GetSubstringData()
dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian
Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")
sDiscription.Substring(CostIndex,
End Sub
Have a look into the Split function of a string. This allows you to split a string into substrings based on a specified delimiting character.
You can then do this:
Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Debug.WriteLine("$" + sfullString.Split("$"c)(3))
Result: $CostCenterId|4^10
You will probably want to do some error checking to make sure the string actually contains the data you expect though.
However looking at the data, what you have is a string containing key-value pairs so you would be better to have a property to hold the CostCenterId and extract the data like this:
Public Property CostCenterId As String
Public Sub Decode(ByVal code As String)
For Each pair As String In code.Split("$"c)
If pair.Length > 0 AndAlso pair.Contains("|") Then
Dim key As String = pair.Split("|"c)(0)
Dim value As String = pair.Split("|"c)(1)
Select Case key
Case "CostCenterId"
Me.CostCenterId = value
End Select
End If
Next
End Sub
Then call it like this:
Decode("PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$")
Why not split() the string by $ into an array, and then look for the element which contains CostCenterId
This should work:
Dim token = "$CostCenterId"
Dim costIndexStart As Integer = sfullString.IndexOf(token)
Dim costIndexEnd As Integer = sfullString.IndexOf("$", costIndexStart + token.Length)
Dim cost As String = sfullString.Substring(costIndexStart, costIndexEnd - costIndexStart + 1)
Result: "$CostCenterId|4^10$"
If you want to omit the dollar-signs:
Substring(costIndexStart + 1, costIndexEnd - costIndexStart - 1)
Try something like this:
Dim CostIndex As Integer
CostIndex = sDiscription.IndexOf("CostCenterId")
auxNum = sDiscription.IndexOf("$"c, CostIndex) - CostIndex
sResult = sDiscription.SubString(CostIndex, auxNum)
Your string,
Dim xString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Substring process,
xString = xString.Substring(xString.IndexOf("$CostCenter"), xString.IndexOf("$", xString.IndexOf("$CostCenter") + 1) - xString.IndexOf("$CostCenter"))
Try this Code:
Dim sfullString = "PaymentMode|NEFT^$IsPaid|False^$Currency|INR-Indian" _
& "Rupee^$CostCenterId|4^10$LedgerId|2^3$"
Dim sp() As String = {"$"}
Dim ar() As String = sfullString.Split(sp, StringSplitOptions.RemoveEmptyEntries)
Array.Sort(ar)
MsgBox("$" & ar(0))
net 2.0 and need to split the last / mark of the string. Currently I have a code that says Dim test As String = "Software\Microsoft\Windows\Welcome" and need a code that will split that Software\Microsoft\Windows\Welcome into two separate section at the end
So I'll have Software\Microsoft\Windows and Welcome as new strings.
I can only find things that will split the beginning from the rest like
Dim whole As String = "Software/Microsoft/Windows/Run"
Dim firstpart As String = whole.Substring(0, whole.IndexOf("/"))
Dim lastpart As String = whole.Substring(whole.IndexOf("/") + 1)`
Use String.LastIndexOf()
Dim whole As String = "Software/Microsoft/Windows/Run"
Dim firstpart As String = whole.Substring(0, whole.LastIndexOf("/"))
Dim lastpart As String = whole.Substring(whole.LastIndexOf("/") + 1)
Try splitting with '\' As your delimeter and store it as a string array. Then just grab the last element and it should be "Welcome".
What I'm trying to do here is Capture 2 Variables from a Textbox
Here is an example of whats going to be in here.
User:Pass
I want to declare everything before the : as user and everything after the : as pass.
I've Googled, and found a few things, but I couldn't seem to get it working fully.
Dim words As String() = textbox1.text.Split(":")
Dim user as String = words(0)
Dim pass as String = words(1)
Dim str = "User:Pass"
Dim split = str.Split(":")
Dim user as String
Dim password as String
If (split.Count = 2) then
user=split(0).ToString()
password = split(1).ToString()
End If
Split on the :, if there are 2 entries in the resulting array, populate the user variable with the first item, and the password variable with the second.
Have a look at the split function.
http://msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.80%29.aspx
Dim user As String
Dim pass As String
Dim iPosEQ As Integer
iPosEQ = textbox1.text.IndexOf(":", System.StringComparison.Ordinal)
kv(0) = textbox1.text.Substring(0, iPosEQ - 1)
kv(1) = textbox1.text.Substring(iPosEQ + 1)
This works even with passwords (or users) with ":"
Here's the deal, it's a log in form and I need to put case-sensitive validation, in other words if your Username = Admin then Admin != admin
Consider this code block (VB is really unfamiliar for me so break it to me gently ^^)
This is after it has matched a record in the database to the parameter passed to the function LogIn()
If dataTable.Rows.Count > 0 Then
'case-sensitive Validation follows
'data in Column ID and Password are placed in variables
'which are then compared to the arguments sent using the Compare() function
Dim strID = dataTable.Columns("U_ID").
Dim strPass = dataTable.Columns("Password")
Dim idResult As Integer 'both results will hold the value of String.Compare()
Dim passwordResult As Integer
*idResult = String.Compare(strID, ID)
the asterisk-ed line returns an error (obviously) as strId is not of data type String.
That's my dilemma.
I also tried using LIKE but again since strId and strPass are not Strings, all I get is an error.
Change this line:
Dim strID = dataTable.Columns("U_ID")
to this:
Dim strID as String = dataTable.Columns("U_ID").ToString