Solved

passing a value with stream reader AFTER a ";"

Posted on 2016-10-19
3
108 Views
Last Modified: 2016-11-03
i have a textfile (table) that has the following values
1;1,2,
2;1,1,
3;2,2,
up to 250;...

what i need is to create a function that, reads the table, and if the user selected 3(or any other number), it  searches within the table (txt file) and passes the value for the selected number  AFTER the ";", (meaning for the example:3 value to pass: 2,2,).

i will use that function value to build another string...

i quite can't get it...

suggestions please

        If True Then
            Dim line As String
            Dim sr As New StreamReader(tableselected)
            Dim table As String = ""

            For x As Integer = 0 To 1000
                line = sr.ReadLine()
                If line Is Nothing Then 'if the file is over, exit the for loop
                    table = table.TrimEnd(","c)

                    Exit For
                End If

                Dim searchWithinThis As String = line
                Dim searchForThis As String = "0;" '= index & ";"

                'If index > 10 Then
                '    searchForThis = index & "0;"
                'End If

                'If index < 10 Then
                '    searchForThis = index & "00;"
                'End If

                Dim tableresult As Boolean = searchWithinThis.Contains(searchForThis)

                If tableresult = True Then
                    Dim range As String = line.Substring(0)
                    ' If index < 10 Then
                    table = table & line.Substring(2) 'line.StartsWith(";") 'line.Substring(0, 41) '& Chr(3)
                    'End If
                    '    If range > 10 Then
                    '    table = table & line.StartsWith(";") 'line.Substring(1, 41) '& Chr(3)
                    'End If
                End If
            Next x
            sr.Close()
return table

Open in new window

0
Comment
Question by:Henry Ottiz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 41851670
    Private Function GetValue(stm As IO.Stream, index As String) As String
        Dim sr As New IO.StreamReader(stm)
        Dim searchWhat = index & ";"
        Do While Not sr.EndOfStream
            Dim line = sr.ReadLine
            If line.StartsWith(searchWhat) Then
                Return line.Substring(searchWhat.Length)
            End If
        Loop
        Return String.Empty
    End Function

Open in new window

0
 

Author Comment

by:Henry Ottiz
ID: 41853018
hi ended up doing differently, used the SPLIT
below code:

            If tbline <> "" Then
                flds = tbline.Split(";")
            End If

            'set each line in the array
            sortplanline(i) = spline
            tableline(x) = tbline

            'PENDING define output of array
            If flds.Length > 0 Then
                ZIPCODE = get_zipSP(i, TBSPlan.Text)

                If ZIPCODE = "" = True Then Continue Do

                sptopass = flds(1) & ZIPCODE 

                If sptopass.Length > 40 = True Then
                    sptopass = Chr(2) & Strings.Left(sptopass, 39) & ".." ' 

Open in new window

0
 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 41853034
Actually there are many ways to parse string: VB.Net: Substring, Split, IndexOf; VB: Left, Mid, Instr etc. AFAIK Substring is fastest. If you'r using split I suggestusingg an array of chars:
flds = tbline.Split(New Char() {";"c}, StringSplitOptions.RemoveEmptyEntries)

Open in new window

In your code any string containing ";", like "3;" or ";" give you 2 elements array - first is {3, empty}, second - {empty, empty}.
If you'll use above code - first give 1 element, second - 0
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 11 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question