Results 1 to 14 of 14

Thread: [RESOLVED] [Vb.net] Split text from txt file and get second value

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Resolved [RESOLVED] [Vb.net] Split text from txt file and get second value

    Previously with .net visual studio 2010 I made this code below a script language to play with her idea was as follows

    read from the txt file the highlighted word in the case below echo and cut the word echo and get the next ones and put it in the variable RUN

    so print, but for some reason my code does not work in .net core is there any specific reason?

    HTML Code:
     For Each line As String In System.IO.File.ReadLines(sfilename)
                ' Display the line.
                Dim execommand = line.ToString
                Dim run As String
                If execommand = "echo" Then
                    run = execommand.Split.ToString(1)
                    MsgBox(run)
                End If
            Next

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [Vb.net] Split text from txt file and get second value

    How dos this make sense?
    vb.net Code:
    1. If execommand = "echo" Then
    2.     run = execommand.Split.ToString(1)
    If you expect to get what comes after "echo" then how does it make sense that the text is equal to "echo", which means there is nothing after it? Surely what you want is a line that starts with "echo", not one that is equal to "echo". If you had actually debugged your code then you'd have seen what was happening. ALWAYS debug your code before posting a question.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [Vb.net] Split text from txt file and get second value

    Actually, not only does that If statement not make sense, the next line doesn't make sense either. Split will return an array and you are calling ToString on that for no apparent reason. There's also little point calling ToString on line either, given that it's already a String. Just start again:
    vb.net Code:
    1. For Each line In File.ReadLines(sfilename)
    2.     If line.StartsWith("echo") Then
    3.         Dim arg = line.Split()(1)
    4.  
    5.         MessageBox.Show(arg)
    6.     End If
    7. Next

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [Vb.net] Split text from txt file and get second value

    I cannot recommend strongly enough that you refrain from leaving parentheses off method calls with no arguments. You simply make your code harder to read and more error prone. Although the line itself is pointless, I would suggest that this:
    vb.net Code:
    1. Dim execommand = line.ToString
    should be this:
    vb.net Code:
    1. Dim execommand = line.ToString()
    That may not seem like much but, if nothing else, it makes it clear that it's a method c all and not a property access. It becomes more important in places like this:
    vb.net Code:
    1. run = execommand.Split.ToString(1)
    At a glance, it looks like you're calling ToString and passing 1 as an argument but that's not what's happening as there is no such overload of ToString. You're actually calling ToString and getting the String result, then indexing that String. That line should be written like this for clarity:
    vb.net Code:
    1. run = execommand.Split().ToString()(1)
    The line is garbage in that it doesn't do anything sensible but, syntax-wise, including the parentheses makes it much clearer what's actually happening.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: [Vb.net] Split text from txt file and get second value

    Quote Originally Posted by jmcilhinney View Post
    I cannot recommend strongly enough that you refrain from leaving parentheses off method calls with no arguments. You simply make your code harder to read and more error prone. Although the line itself is pointless, I would suggest that this:
    vb.net Code:
    1. Dim execommand = line.ToString
    should be this:
    vb.net Code:
    1. Dim execommand = line.ToString()
    That may not seem like much but, if nothing else, it makes it clear that it's a method c all and not a property access. It becomes more important in places like this:
    vb.net Code:
    1. run = execommand.Split.ToString(1)
    At a glance, it looks like you're calling ToString and passing 1 as an argument but that's not what's happening as there is no such overload of ToString. You're actually calling ToString and getting the String result, then indexing that String. That line should be written like this for clarity:
    vb.net Code:
    1. run = execommand.Split().ToString()(1)
    The line is garbage in that it doesn't do anything sensible but, syntax-wise, including the parentheses makes it much clearer what's actually happening.

    Yes I also thought that this would not return any value in 2010 but at the time I was able to always receive the following value, the logic of the function is:

    dim exampleVBForum as string = file.txt ' example
    dim Result as string
    if line contains( "echo") then
    result = line.tostring.split(1) ' this would cut the word echo from the beginning
    msgbox(result)
    end if

    clarifying I remembered now that I used contains and not equality because equality would never return the value, but the split was used in a way that based on the example above would cut the first word to the variable line.


    I was wrong about equality, but the logic was not wrong, it was useless.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [Vb.net] Split text from txt file and get second value

    Code:
    result = line.split()(1) ' this would cut the word echo from the beginning correctly, but it'll fail if the split returns 1 or 0 elements
    Try...

    Code:
    result = line.split().LastOrDefault

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: [Vb.net] Split text from txt file and get second value

    Quote Originally Posted by LiwisJames View Post
    Yes I also thought that this would not return any value in 2010 but at the time I was able to always receive the following value, the logic of the function is:

    dim exampleVBForum as string = file.txt ' example
    dim Result as string
    if line contains( "echo") then
    result = line.tostring.split(1) ' this would cut the word echo from the beginning
    msgbox(result)
    end if

    clarifying I remembered now that I used contains and not equality because equality would never return the value, but the split was used in a way that based on the example above would cut the first word to the variable line.


    I was wrong about equality, but the logic was not wrong, it was useless.
    I just found the command, to create a mini script language make good use of it:


    For Each line As String In System.IO.File.ReadLines(sfilename)
    ' Display the line.
    Dim execommand = line.ToString()
    Dim run As String
    ' Console.WriteLine("-- {0}", line)
    If execommand.Contains("echo") Then
    run = execommand.Split.Skip(1)(0)
    MsgBox(run)
    End If
    Next

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [Vb.net] Split text from txt file and get second value

    Quote Originally Posted by LiwisJames View Post
    but the logic was not wrong
    Really? This is what you said you used before, and it makes some sense:
    Code:
    result = line.tostring.split(1)
    This is what you posted originally:
    Code:
    run = execommand.Split.ToString(1)
    As I said previously, that ToString call has no business being there, because that is creating a String from your array. The logic you intended to implement may have been sound, but the logic you actually implemented made little sense.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [Vb.net] Split text from txt file and get second value

    Code:
    For Each line As String In System.IO.File.ReadLines(sfilename)
        ' Display the line.
        Dim execommand = line 'already is a string
        Dim run As String
        ' Console.WriteLine("-- {0}", line)
        If execommand.Contains("echo") Then
            run = execommand.Split.Skip(1).FirstOrDefault 'safer method
            MsgBox(run)
        End If
    Next

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: [Vb.net] Split text from txt file and get second value

    Quote Originally Posted by jmcilhinney View Post
    Really? This is what you said you used before, and it makes some sense:
    Code:
    result = line.tostring.split(1)
    This is what you posted originally:
    Code:
    run = execommand.Split.ToString(1)
    As I said previously, that ToString call has no business being there, because that is creating a String from your array. The logic you intended to implement may have been sound, but the logic you actually implemented made little sense.
    Thanks for the help it was very useful what you told me to come up with the solution, well I was trying to see which first value of the string if it was equal to echo then cut it and get the rest of the string like the code below:

    Code:
    'run = execommand.Split.Skip(1)(0)
     run = execommand.Split.Skip(1).FirstOrDefault 'safer method
    Your comments were essential for this as I used it for a long time I couldn't remember how it was done.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2021
    Posts
    166

    Re: [Vb.net] Split text from txt file and get second value

    Quote Originally Posted by .paul. View Post
    Code:
    For Each line As String In System.IO.File.ReadLines(sfilename)
        ' Display the line.
        Dim execommand = line 'already is a string
        Dim run As String
        ' Console.WriteLine("-- {0}", line)
        If execommand.Contains("echo") Then
            run = execommand.Split.Skip(1).FirstOrDefault 'safer method
            MsgBox(run)
        End If
    Next
    thanks for the help, this way of declaring the first value [.Split.Skip(1).FirstOrDefault ] that always sets the first parameter as default was very important and preventing possible capture errors of a possible echo inside the text.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] [Vb.net] Split text from txt file and get second value

    It's Skip(1) that avoids the 'echo' part. FirstOrDefault takes the second element if it exists

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] [Vb.net] Split text from txt file and get second value

    Using Contains is a bad idea because that will match if the specified substring is found anywhere in the text. If what you're actually looking for is text that starts with that substring then you should use StartsWith. In fact, .StartsWith("echo ") is probably what you ought to be using, because that's really what you're interested in. I'd also suggest using the overload of Split that discards empty entries, because there is the possibility that someone will include multiple spaces between the command and the argument.

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: [RESOLVED] [Vb.net] Split text from txt file and get second value

    Code:
    Console.WriteLine("-- {0}", line)
    String interpolation:
    Code:
    Console.WriteLine($"-- {line}")

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width