.Substring problem from text file. [HELP]-VBForums
Results 1 to 8 of 8

Thread: .Substring problem from text file. [HELP]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    17

    .Substring problem from text file. [HELP]

    H1:1023 H2:1022 H3:1021 02.01.2018

    H1:100 H2:100 H3:100 02.01.2018

    Above is my logs..

    Code:
    Dim path As String = "D:\tatlo.txt"
            Dim readText() As String = File.ReadAllLines(path)
            Dim s As String
            Dim Total As Integer
            Dim Lines As Integer = 0
            For Each s In readText
                If s.EndsWith("02.01.2018") Or s.EndsWith("02.01.2018 Sprinkler_On") Or s.EndsWith("02.01.2018 Light_On") Or s.EndsWith("02.01.2018 Sprinkler_Light_On") Then
                    Lines += 1
                    Total += (Integer.Parse(Val(s.Trim.Substring(3, 4))) + Integer.Parse(Val(s.Trim.Substring(11, 4))) + Integer.Parse(Val(s.Trim.Substring(19, 4))))
                End If
                TextBox1.Text = Lines
                TextBox2.Text = Total
    
            Next
    In this problem. I have successfully added the first line of my logs. But I'm having a problem with my second line? What if it became 3-Digits? I cannot add the second line because it isn't aligned with the first line. What do I need to do? Please help.

    My output should be.. Total is equal to 3366
    TextBox2.text = Total

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,100

    Re: .Substring problem from text file. [HELP]

    Unfortunately, you posted the question in your previous thread before starting this thread so I answered the question there.
    http://www.vbforums.com/showthread.p...=1#post5248373

    Quote Originally Posted by pogiirojen View Post
    H1:1023 H2:1022 H3:1021 02.01.2018

    H1:100 H2:100 H3:100 02.01.2018
    ...
    My output should be.. Total is equal to 3366
    ...
    By the way, using the values above I get 3369 using the function I posted, rather than 3366, so I'll have to see why that is.
    Last edited by passel; Jan 1st, 2018 at 09:07 AM.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,100

    Re: .Substring problem from text file. [HELP]

    I found the problem, I should have started the search using StartPos + 1, rather than StartPos. Using StartPos just kept finding the first field over and over, rather than finding the "next" field.
    Here is the corrected example.
    Code:
    Public Class Form1
        Dim StringArray() As String = {"H1:1023 H2:1022 H3:1021 02.01.2018", "H1:100 H2:100 H3:100 02.01.2018"}
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim total As Integer
            For i As Integer = 0 To 1
                total += SelectField(1, StringArray(i)) + SelectField(2, StringArray(i)) + SelectField(3, StringArray(i))
                Debug.Print(total.ToString)
            Next
    
        End Sub
        Private Function SelectField(Idx As Integer, Input As String) As Integer
            Dim startPos As Integer = 1
            Dim endPos As Integer
            For i As Integer = 1 To Idx
                startPos = InStr(startPos + 1, Input, ":")
            Next
            endPos = InStr(startPos, Input, " ") - 1
            Return CInt(Val(Input.Substring(startPos, endPos - startPos)))
        End Function
    End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    17

    Re: .Substring problem from text file. [HELP]

    I got the point & idea. But you used
    Code:
     Dim StringArray() As String = {"H1:1023 H2:1022 H3:1021 02.01.2018", "H1:100 H2:100 H3:100 02.01.2018"}
    Becuase in the system I'am making. I used
    Code:
     Dim readText() As String = Form1.TextBox1.Text.Split(Environment.NewLine)
    Is it possible? The textbox contains the two lines which is

    H1:1023 H2:1022 H3:1021 02.01.2018

    H1:100 H2:100 H3:100 02.01.2018

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    17

    Re: .Substring problem from text file. [HELP]

    It will be like this.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim StringArray() As String = TextBox1.Text.Split(Environment.NewLine)
            Dim total As Integer
            For i As Integer = 0 To 1
                total += SelectField(1, StringArray(i)) + SelectField(2, StringArray(i)) + SelectField(3, StringArray(i))
                Debug.Print(total.ToString)
                TextBox1.Text = total
            Next
        End Sub
        Private Function SelectField(ByVal Idx As Integer, ByVal Input As String) As Integer
            Dim startPos As Integer = 1
            Dim endPos As Integer
            For i As Integer = 1 To Idx
                startPos = InStr(startPos + 1, Input, ":")
            Next
            endPos = InStr(startPos, Input, " ") - 1
            Return CInt(Val(Input.Substring(startPos, endPos - startPos)))
        End Function
    I tried using it and i got an error called "Argument 'Start' must be greater than zero." Help me pls

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,100

    Re: .Substring problem from text file. [HELP]

    Why wouldn't it be more like your original code, instead of my test loop?
    You are looping through the strings, with s representing the current string you're checking. You would pass the string, s, to the parser.
    Code:
    Dim path As String = "D:\tatlo.txt"
            Dim readText() As String = File.ReadAllLines(path)
            Dim s As String
            Dim Total As Integer
            Dim Lines As Integer = 0
            For Each s In readText
                If s.EndsWith("02.01.2018") Or s.EndsWith("02.01.2018 Sprinkler_On") Or s.EndsWith("02.01.2018 Light_On") Or s.EndsWith("02.01.2018 Sprinkler_Light_On") Then
                    Lines += 1
                    Total += SelectField(1, s) + SelectField(2, s) + SelectField(3, s)
                End If
                TextBox1.Text = Lines
                TextBox2.Text = Total
    
            Next
    
    '.....
    
        Private Function SelectField(ByVal Idx As Integer, ByVal Input As String) As Integer
            Dim startPos As Integer = 1
            Dim endPos As Integer
            For i As Integer = 1 To Idx
                startPos = InStr(startPos + 1, Input, ":")
            Next
            endPos = InStr(startPos, Input, " ") - 1
            Return CInt(Val(Input.Substring(startPos, endPos - startPos)))
        End Function

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    17

    Re: .Substring problem from text file. [HELP]

    In the first place sir. I used
    Code:
    Dim path As String = "D:\tatlo.txt"
            Dim readText() As String = File.ReadAllLines(path)
    but now my original code is using this code
    Code:
    Dim StringArray() As String = TextBox1.Text.Split(Environment.NewLine)
    I mean. Instead of using path as my basis. I load the text file into a textbox1 using OpenFileDialog, then. I use the textbox1 as my basis.

    whenever I used my original code
    Code:
    Dim StringArray() As String = TextBox1.Text.Split(Environment.NewLine)
    it keeps saying i got an error called "Argument 'Start' must be greater than zero."

    vise versa. using the path. it works.

    In my requirement paper. It said. Use textboxes not paths or text files as basis. Thank you. Please help me sir.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,100

    Re: .Substring problem from text file. [HELP]

    What line gives you that error?
    When you use Split, you might be getting an empty string in your StringArray. You shouldn't be passing a badly formatted string to the parser.
    Or you can change the parser to return 0 if it fails because you passed it an illegal string (i.e. a string that doesn't contain the number of ":" characters you asked it to search for.

    For example, if you asked it to give you the second field, and your string didn't have any ":", in it, startPos would be set to 0 when you tried to look for the first, and then the search for the second would fail the way you indicated because you would be passing a 'Start' argument of 0 to the Instr function.

    A "silent" failure could be handled like this
    Code:
        Private Function SelectField(ByVal Idx As Integer, ByVal Input As String) As Integer
            Dim startPos As Integer = 1
            Dim endPos As Integer
            For i As Integer = 1 To Idx
                startPos = InStr(startPos + 1, Input, ":")
                If startPos = 0 Then
                  Return 0    'Abandon the search, return zero
                End If
            Next
            endPos = InStr(startPos, Input, " ") - 1
            Return CInt(Val(Input.Substring(startPos, endPos - startPos)))
        End Function
    Last edited by passel; Jan 1st, 2018 at 11:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.