-
Jun 2nd, 2021, 10:20 PM
#1
Thread Starter
Addicted Member
[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
-
Jun 2nd, 2021, 10:26 PM
#2
Re: [Vb.net] Split text from txt file and get second value
How dos this make sense?
vb.net Code:
If execommand = "echo" Then
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.
-
Jun 2nd, 2021, 10:30 PM
#3
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:
For Each line In File.ReadLines(sfilename)
If line.StartsWith("echo") Then
Dim arg = line.Split()(1)
MessageBox.Show(arg)
End If
Next
-
Jun 2nd, 2021, 10:47 PM
#4
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:
Dim execommand = line.ToString
should be this:
vb.net Code:
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:
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:
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.
-
Jun 3rd, 2021, 12:28 AM
#5
Thread Starter
Addicted Member
Re: [Vb.net] Split text from txt file and get second value
Originally Posted by jmcilhinney
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:
Dim execommand = line.ToString
should be this:
vb.net Code:
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:
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:
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.
-
Jun 3rd, 2021, 12:34 AM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 3rd, 2021, 12:46 AM
#7
Thread Starter
Addicted Member
Re: [Vb.net] Split text from txt file and get second value
Originally Posted by LiwisJames
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
-
Jun 3rd, 2021, 02:37 AM
#8
Re: [Vb.net] Split text from txt file and get second value
Originally Posted by LiwisJames
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.
-
Jun 3rd, 2021, 03:08 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 3rd, 2021, 05:36 PM
#10
Thread Starter
Addicted Member
Re: [Vb.net] Split text from txt file and get second value
Originally Posted by jmcilhinney
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.
-
Jun 3rd, 2021, 05:38 PM
#11
Thread Starter
Addicted Member
Re: [Vb.net] Split text from txt file and get second value
Originally Posted by .paul.
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.
-
Jun 3rd, 2021, 06:04 PM
#12
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 3rd, 2021, 08:46 PM
#13
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.
-
Jun 7th, 2021, 04:13 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|