Let's say I have for example a simple three line text file where is stated for example:
FirstName=Adam
LastName=Smith
Age=25
but there are more files with different value and/or are scrambled that the LastName is first, FirstName is second etc. so the app must not be dependant on the line number but instead read words from the text.
I want that app to get into file, detect that the Adam is first name, Smith is last name, that the age is 25 and output it as a strings that then get imported and immediately assigned to appropriate in-app variable (LastName=Smith gets assigned to LastName variable in App etc.).
I hope you understood me.. it is hard to explain :-). I can try to provide further explanation if u can't get what I really want.
Thanks in advance
You need to use some string functions. What you need are Substring and IndexOf. Substring
is used to get a certain part of the string. IndexOf
is used to get a location of a certain part in a string. So you need to get where the equal sign is then separate the string before and after the equal sign. The one before the equal sign is the property name while the one after the equal sign is the property value.
Sub Main
Dim firstName As String = "", lastName As String = "", age As Integer
For Each line In File.ReadLines("C:\input.txt")
Dim equalSignIndex = line.IndexOf("=")
Dim propertyName = line.Substring(0, equalSignIndex)
Dim propertyValue = line.Substring(equalSignIndex + 1, line.Length - equalSignIndex - 1)
Select Case propertyName
Case "FirstName" :
firstName = propertyValue
Case "LastName" :
lastName = propertyValue
Case "Age" :
age = propertyValue
End Select
Next
Console.WriteLine("Name = {0} {1}, Age = {2}", firstName, lastName, age)
End Sub