Phil_C Posted April 24, 2003 Posted April 24, 2003 I use the following code to create and populate an array Dim objSR1 As StreamReader, oldSize1 As Integer, str6 As String With OpenFileDialog1 .DefaultExt = "txt" .FileName = strFileName .Filter = "Text Files (*.txt)/*.txt/All Files (*.*)/*.*" .FilterIndex = 1 .InitialDirectory = "C:\Temp\" .Title = "Open Existing text file" End With OpenFileDialog1.ShowDialog() Dim str As System.IO.Stream str = OpenFileDialog1.OpenFile objSR1 = New StreamReader(str) Do While objSR1.Peek <> -1 If Open2 Is Nothing Then oldSize1 = -1 Else oldSize1 = UBound(Open2) End If ReDim Preserve Open2(oldSize1 + 1) Open2(oldSize1 + 1) = objSR1.ReadLine Loop objSR1.Close() 'Close file objSR1 = Nothing 'destroy the object Then I use the following code to search the array for a key that is set in a buttonclick sub Public Function GetValue(ByVal keyGet As String) As String 'Given key name keyGet, return corresponding value string Dim i As Integer Dim keyFound As Boolean Dim keyDesc As Boolean Dim keyStr As String Dim keyStr2 As String keyFound = False keyDesc = False For i = 0 To Open2.Length - 1 If Not (keyDesc) Then keyStr = Open2(i).Substring(0, Open2(i).IndexOf(" ") ) If keyStr.Equals(keyGet) Then keyFound = True If keyStr.Equals("description") Then keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf("""")) End If If keyStr.Equals("licence") Then keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf("""")) End If If keyFound Then GetValue = Open2(i).Substring(Open2(i).IndexOf(" ") + 1) End If Else keyDesc = (Open2(i).IndexOf("""") < 0) If keyFound Then GetValue &= Chr(13) & Chr(10) & Open2(i) ' 13 = carriage return 10 = line feed End If End If If keyFound And Not (keyDesc) Then Exit For Next i If Not (keyFound) Then GetValue = String.Empty End Function The problem with the above code is that if it finds a line in the array without a space it returns a value cannot be less than zero error and hightlights this line keyStr = Open2(i).Substring(0, Open2(i).IndexOf(" ") ) How would I modify the code to skip lines with no spaces? Thank you Phil C Quote
*Experts* Nerseus Posted April 24, 2003 *Experts* Posted April 24, 2003 Well first, I'd probably rather use XML to store this kind of thing. But, I'd probably not even put the blank lines in my array. I'd first change the top bit of code to the following: Dim sLine As String Do While objSR1.Peek <> -1 sLine = objSR1.ReadLine If sLine.Length > 0 Then If Open2 Is Nothing Then oldSize1 = -1 Else oldSize1 = UBound(Open2) End If ReDim Preserve Open2(oldSize1 + 1) Open2(oldSize1 + 1) = objSR1.ReadLine End If Loop -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Phil_C Posted April 24, 2003 Author Posted April 24, 2003 Thanks Nerseus I cannot use xml as the text files the code reads from do not have an equal sign separating the keys and their values only a space. I have no control over the arrangement of the text files. Your modification of the code works only if the the line is empty. The problem is if the line contains a word and no space the error occurs. Thanks Phil Quote
*Experts* Nerseus Posted April 24, 2003 *Experts* Posted April 24, 2003 Ah, well what do you want to do with lines that don't have a space? Do you need them in your array for something else or can you exclude them? If you want to exclude them, simply change the line in my original Loop to use IndexOf(" ") > 0. Otherwise, why not just do the IndexOf check before trying to use it in the substring? Public Function GetValue(ByVal keyGet As String) As String 'Given key name keyGet, return corresponding value string Dim i As Integer Dim keyFound As Boolean Dim keyDesc As Boolean Dim keyStr As String Dim keyStr2 As String keyFound = False keyDesc = False For i = 0 To Open2.Length - 1 If Not (keyDesc) Then If Open2(i).IndexOf(" ") > 0 Then keyStr = Open2(i).Substring(0, Open2(i).IndexOf(" ") ) If keyStr.Equals(keyGet) Then keyFound = True If keyStr.Equals("description") Then keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf("""")) End If If keyStr.Equals("licence") Then keyDesc = (Open2(i).IndexOf("""") > -1) And (Open2(i).IndexOf("""") = Open2(i).LastIndexOf("""")) End If If keyFound Then GetValue = Open2(i).Substring(Open2(i).IndexOf(" ") + 1) End If End If Else keyDesc = (Open2(i).IndexOf("""") < 0) If keyFound Then GetValue &= Chr(13) & Chr(10) & Open2(i) ' 13 = carriage return 10 = line feed End If End If If keyFound And Not (keyDesc) Then Exit For Next i If Not (keyFound) Then GetValue = String.Empty End Function -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Drstein99 Posted September 27, 2003 Posted September 27, 2003 I think if I understand correctly me and you are experiencing the same problems. I am trying to get the position of a certain string of text within a string. a simple indexof will do the trick, if its a string with no cr/lf's in it. the indexof function skips over cr/lfs and gives incorrect location of the string. Quote www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
Phil_C Posted September 28, 2003 Author Posted September 28, 2003 Hello Drstein99 Nerseus method worked fine. Although later on I used regular expressions to find the strings. Regards Phil Quote
*Experts* Nerseus Posted September 28, 2003 *Experts* Posted September 28, 2003 Egads! IndexOf does NOT skip over CR/LF characters! Twice in one day :) -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.